java - how to use utility class to start intent android -
i updating code company app , there 20 activity classes download pdf , display using code:
public void showpdf() { file file = new file(environment.getexternalstoragedirectory()+"/pdf/read.pdf"); packagemanager packagemanager = getpackagemanager(); intent testintent = new intent(intent.action_view); testintent.settype("application/pdf"); list list = packagemanager.queryintentactivities(testintent, packagemanager.match_default_only); intent intent = new intent(); intent.setaction(intent.action_view); uri uri = uri.fromfile(file); intent.setdataandtype(uri, "application/pdf"); startactivity(intent); }
the code working, has been replicated in 20 classes (seems bad me) , put single class each activity class imports, when try this, things getpackagemanager()
, startactivity(intent)
no longer work.
how can structure class make happen? or going wrong way.
public class pdfutlity{ public static void showpdf(context context) { file file = new file(environment.getexternalstoragedirectory()+"/pdf/read.pdf"); packagemanager packagemanager = context.getpackagemanager(); intent testintent = new intent(intent.action_view); testintent.settype("application/pdf"); list list = packagemanager.queryintentactivities(testintent, packagemanager.match_default_only); intent intent = new intent(); intent.setaction(intent.action_view); uri uri = uri.fromfile(file); intent.setdataandtype(uri, "application/pdf"); ((activity)context).startactivity(intent); } } public class myactivity extends activity{ @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); ///..... pdfutlity.showpdf(this); } }
Comments
Post a Comment