Learn Android together how to get a list of phone programs and program-related information and start the specified program (26)

Source: Internet
Author: User

List of programs:


Start the program and get the program information:







The code is as follows:

Create a appinfo class to represent the application

<pre name= "code" class= "Java" >public class AppInfo {public charsequence title;//program name Public charsequence PackageName; Package name Intent intent;//start intentpublic drawable icon;//program icon/* * Set to start the program intent */final void Setactivity (componentname cla ssname, int launchflags) {intent = new intent (Intent.action_main); intent.addcategory (Intent.category_launcher); Intent.setcomponent (ClassName); Intent.setflags (launchflags);}}


To create an adapter for a list of programs:

/** * Program List Adapter * @author Bill * */public class Showapplistadapter extends Baseadapter {private arraylist<appinfo> app List;private layoutinflater inflater;public showapplistadapter (Context context,arraylist<appinfo> appList, Packagemanager pm) {this.applist = Applist;inflater = Layoutinflater.from (context);} public int GetCount () {return applist.size ();} Public Object getItem (int position) {return applist.get (position);} public long getitemid (int position) {return position;} Public View GetView (int position, View Convertview, ViewGroup parent) {Final AppInfo info = applist.get (position); Viewholder holder = null;if (null = = Convertview) {Convertview = inflater.inflate (R.layout.app_list_item, null); holder = New Viewholder (); holder.lv_image = (ImageView) Convertview.findviewbyid (r.id.lv_icon); holder.lv_name = (TextView) Convertview.findviewbyid (r.id.lv_item_appname); holder.lv_packname = (TextView) Convertview.findviewbyid (R.id.lv_ Item_packageame); Convertview.settag (holder);} else {HoldeR = (viewholder) Convertview.gettag ();} Holder.lv_image.setImageDrawable (Info.icon); final charsequence name = info.title;final charsequence Packname = Info.packagename;holder.lv_name.settext (name); Holder.lv_packname.setText (packname); return convertview;} Private final static class Viewholder{imageview lv_image; TextView Lv_name; TextView lv_packname;}}




public class Mainactivity extends Activity {/* * Application collection */private arraylist<appinfo> appinfos;private ListView lv_ap p;/* * Manages the application package and obtains program information through it */private packagemanager pm; @Overrideprotected void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (r.layout.app_list);p m = Getpackagemanager (); InitView (); new Thread (runable). Start ();} private void Initview () {Lv_app = (ListView) Findviewbyid (R.id.app_list_view); Lv_app.setonitemclicklistener (new Appdetaillinster ());} Private final Runnable runable = new Runnable () {public void run () {loadapplications (); Myhandler.obtainmessage (). Sendtotarget ();}}; Private Handler MyHandler = new Handler () {@Overridepublic void Handlemessage (Message msg) {lv_app.setadapter (new Showapp ListAdapter (Mainactivity.this,appinfos, PM));}};/ * * Load app list */private void Loadapplications () {Packagemanager manager = This.getpackagemanager (); Intent mainintent = new I Ntent (Intent.action_main, null); Mainintent.addcategory (Intent.category_lauNcher); final list<resolveinfo> apps = manager.queryintentactivities (mainintent, 0); Collections.sort (Apps, New Resolveinfo.displaynamecomparator (manager)), if (apps! = null) {final int count = Apps.size (); if (Appinfos = = null) {Appinfos = new arraylist<appinfo> (count);} Appinfos.clear (); for (int i = 0; i < count; i++) {AppInfo application = new AppInfo (); ResolveInfo info = apps.get (i); application.title = Info.loadlabel (manager); Application.setactivity (New componentname (Info.activityinfo.applicationinfo.packagename,info.activityinfo.name), intent.flag_activity_new_task| intent.flag_activity_reset_task_if_needed); Application.icon = Info.activityInfo.loadIcon (manager); Application.packagename = Info.activityinfo.applicationinfo.packagename;appinfos.add (application);}} /** * List Listener class * @author Bill * */public final class Appdetaillinster implements Onitemclicklistener {Alertdialog Dialog;publ IC void Onitemclick (adapterview<?> view, view arg1,final int position, long Arg3) {AlErtdialog.builder Builder = new Alertdialog.builder (mainactivity.this); Builder.settitle ("Options"); Builder.setitems ( R.array.choice, New Onclicklistener () {public void OnClick (Dialoginterface dialog, int which) {final AppInfo AppInfo = App  Infos.get (position); switch (which) {case 0://Start program try {startApp (appInfo);} catch (Exception e) {}break;case 1://Details try {Showappdetail (appInfo);} catch (Exception e) {}break;} Dialog.dismiss ();} private void Showappdetail (AppInfo AppInfo) throws Exception {final String packname = appInfo.packageName.toString (); Final PackageInfo packinfo = Getapppackinfo (packname); final String versionname = packinfo.versionname;final String[] Apppremissions = packinfo.requestedpermissions;final String appName = appInfo.title.toString (); Intent Showdetailintent = new Intent (mainactivity.this,showappdetailactivity.class); Bundle bundle = new Bundle (), bundle.putstring ("PackageName", Packname), bundle.putstring ("appversion", versionname); Bundle.putstringarray ("Apppremissions", APPPremissions); Bundle.putstring ("appname", appname); Showdetailintent.putextras (bundle); StartActivity ( showdetailintent);} private void StartApp (AppInfo AppInfo) throws Exception {final String packname = appInfo.packageName.toString (); final String activityname = Getactivityname (Packname), if (null = = Activityname) {toast.maketext (mainactivity.this, "program cannot Start", Toast.length_short); return;} Intent Intent = new Intent () intent.setcomponent (new ComponentName (Packname,activityname)); startactivity (Intent);}); Dialog = Builder.create ();d ialog.show ();}} /** * Get program information * @param packname * @return * @throws Exception */public packageinfo getapppackinfo (String packname) throws E Xception {return Pm.getpackageinfo (Packname, packagemanager.get_activities| packagemanager.get_permissions);} /** * Get activity to start related programs * @param packname * @return * @throws Exception */public string getactivityname (string packname) t Hrows Exception {final PackageInfo packinfo = Pm.getpackageinfo (packname,packagemanager.get_activities); FinaL activityinfo[] Activitys = packinfo.activities;if (Null = = Activitys | | activitys.length <= 0) {return null;} return activitys[0].name;}}


App_list.xml:

<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:layout_width=" fill_parent "    android:layout_height=" fill_parent "    android:o rientation= "vertical"    android:background= "@android: Color/black" >    <listview android:id=        "@+id/ App_list_view "        android:layout_width=" fill_parent "        android:layout_height=" fill_parent "      >    </ListView></RelativeLayout>



App_list_item.xml:

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "android:orientation=" horizontal "android:layout_width=" Fill_parent "android:layout_height=" Wrap_content " Android:gravity= "center_vertical" ><imageviewandroid:id= "@+id/lv_icon" android:layout_width= "48px" Android: layout_height= "48px" android:layout_margintop= "5px" android:layout_marginbottom= "5px" ></ImageView>< linearlayoutandroid:orientation= "vertical" android:layout_width= "wrap_content" android:layout_height= "48px" android:paddingleft= "5px" ><textviewandroid:id= "@+id/lv_item_appname" android:layout_width= "Fill_parent" android:layout_height= "Wrap_content" android:singleline= "true" android:textsize= "16px" android:textstyle= "bold" Android:textcolor= "#fff" ></textview><textviewandroid:id= "@+id/lv_item_packageame" Android:layout_ Width= "Fill_parent" android:layout_height= "Wrap_content" android:singleline= "true" android:textcolor= "#fff" > </TextView></linearlayout></linearlayout> 



/** * View app information * @author Bill * */public class Showappdetailactivity extends Activity {private TextView tv_appname;private T Extview tv_appversion;private TextView tv_packagename;private TextView tv_permission; @Overrideprotected void onCreate (Bundle savedinstancestate) {//TODO auto-generated method Stubsuper.oncreate (savedinstancestate); Setcontentview (r.layout.app_detial); tv_ AppName = (TextView) Findviewbyid (r.id.detail_app_name); tv_appversion = (TextView) Findviewbyid (r.id.detail_app_ version); tv_packagename = (TextView) Findviewbyid (r.id.detail_app_packname); tv_permission = (TextView) Findviewbyid ( R.id.detail_app_permissions); Bundle bundle = This.getintent (). Getextras (); String packagename= bundle.getstring ("PackageName"); String appversion = bundle.getstring ("appversion"); String appname = bundle.getstring ("appname"); string[] apppremissions = Bundle.getstringarray ("apppremissions"); StringBuilder sb = new StringBuilder (); for (String s:apppremissions) {sb.append (s); Sb.append ("\ n");} Tv_appnAme.settext (appname); Tv_appversion.settext (appversion); Tv_packagename.settext (PackageName); tv_ Permission.settext (Sb.tostring ());}}

App_detial.xml:

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/    Android "android:orientation=" vertical "android:layout_width=" fill_parent "android:layout_height=" Fill_parent " > <tablelayout android:id= "@+id/app_table" android:layout_width= "Fill_parent" Android:layo ut_height= "Wrap_content" > <tablerow android:id= "@+id/tablerow1" android:layout_width= "WR Ap_content "android:layout_height=" wrap_content "> <textview android:la         Yout_width= "Wrap_content" android:layout_height= "wrap_content" android:text= "program name"/> <TextView        Android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:id= "@+id/detail_app_name"/> </TableRow> <tablerow android:id= "@+id/tablerow2" android:layout_width= "WR Ap_content "Android:layout_height= "Wrap_content" > <textview android:layout_width= "wrap_content" android:layout_height= "Wrap_co Ntent "android:text=" program version "/> <textview android:layout_width=" Wrap_content "Android:layout_heig ht= "Wrap_content" android:id= "@+id/detail_app_version"/> </TableRow> <tablerow Andro     Id:id= "@+id/tablerow3" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" >     <textview android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:text= "package name" /> <textview android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android:i d= "@+id/detail_app_packname"/> </TableRow> <tablerow android:id= "@+id/tablerow4" a                  Ndroid:layout_width= "Wrap_content" android:layout_height= "wrap_content" > <textview Android:layout_widtH= "Wrap_content" android:layout_height= "wrap_content" android:text= "program permissions"/> <textview Android : layout_width= "wrap_content" android:layout_height= "wrap_content" android:id= "@+id/detail_app_permissions"/&             Gt </TableRow> </TableLayout></LinearLayout>

Finally, don't forget to configureAndroidmanifest.



Reprint Please specify source:http://blog.csdn.net/hai_qing_xu_kong/article/details/44274387 Emotional Control _

Learn Android together how to get a list of phone programs and program-related information and start the specified program (26)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.