The main thing in launcher is to get the portals and icons for all the app lists, and there are two ways to get them:
PackageInfo
ResolveInfo
Run get launcher of all apps and allow click events to go to the app
The following two methods are used to obtain a list of all apps:
Establish basic data:
Pakagemod.java
public class Pakagemod {public String pakagename;public string appname;public drawable icon;public pakagemod () {super ();} Public Pakagemod (String pakagename, String appName, drawable icon) {super (); this.pakagename = Pakagename;this.appname = A Ppname;this.icon = icon;}}
To build the adapter:
public class Demogridadapter extends Baseadapter {private layoutinflater inflater;private list<pakagemod> datas; Public Demogridadapter (context context, list<pakagemod> datas) {super (); inflater = Layoutinflater.from (context) ; this.datas = datas;} @Overridepublic int GetCount () {return datas.size ();} @Overridepublic Object getItem (int position) {return null;} @Overridepublic long Getitemid (int position) {return position;} @Overridepublic view GetView (int position, view Convertview, ViewGroup parent) {Viewholder holder;if (Convertview = = null) {//Using the view's object Itemview associated with R.layout.item Convertview = inflater.inflate (R.layout.apps, null); holder = new Viewholder (); Holder.icon = (ImageView) Convertview.findviewbyid (r.id.apps_image); Holder.label = (TextView) Convertview.findviewbyid (R.id.apps_textview); Convertview.settag (holder);} else {holder = (Viewholder) Convertview.gettag ();} Holder.icon.setImageDrawable (Datas.get (position). icon); Holder.label.setText (Datas.get (position). AppName); Return COnvertview;} Class Viewholder {private ImageView icon;private TextView label;}}
To create a view of the adapter:
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:layout_width=" fill_parent " android:layout_height=" fill_parent " android:gravity=" Center " android:orientation=" vertical "> <imageview android:id=" @+id/apps_image " android: Layout_width= "48dip" android:layout_height= "48dip" android:icon= "@drawable/ic_launcher"/> < TextView android:id= "@+id/apps_textview" android:layout_width= "wrap_content" android:layout_ height= "Wrap_content" android:maxems= "5" android:maxlines= "1" android:text= "good"/></ Linearlayout>
Below the activity to get a list and display to the GridView, and click into the app;
Using PackageInfo
public class Packageinfodemo extends Activity {private GridView gridview;private Packagemanager pmanager;private List <PakageMod> datas;private String tag = "mainactivity"; @Overrideprotected void OnCreate (Bundle savedinstancestate {super.oncreate (savedinstancestate); This.setcontentview (r.layout.activity_main);//Acquired Gridviewgridview = ( GridView) Findviewbyid (R.id.gridview);//Get picture, application name, package name Pmanager = PackageInfoDemo.this.getPackageManager (); list<packageinfo> applist = Getallapps (packageinfodemo.this);d atas = new arraylist<pakagemod> (); for (int i = 0; I < applist.size (); i++) {PackageInfo pinfo = Applist.get (i); Pakagemod Shareitem = new Pakagemod ();//Set Picture Shareitem.icon = Pmanager.getapplicationicon (pinfo.applicationinfo);// Set Application Name Shareitem.appname = Pmanager.getapplicationlabel (pinfo.applicationinfo). toString ();// Set the application's package name Shareitem.pakagename = Pinfo.applicationinfo.packagename;datas.add (Shareitem);} Gridview.setadapter (New Baseadapter (this, datas));//Click Apply icon to respond to GRIDview.setonitemclicklistener (New Clicklistener ());} public static list<packageinfo> Getallapps (context context) {list<packageinfo> apps = new arraylist< Packageinfo> (); Packagemanager Pmanager = Context.getpackagemanager ();//Get all apps in the phone list<packageinfo> packlist = Pmanager.getinstalledpackages (0); for (int i = 0; i < packlist.size (); i++) {PackageInfo Pak = (packageinfo) packlist.ge T (i);//If the value in the IF () <=0 is a program of its own, otherwise systems engineering comes with if ((Pak.applicationInfo.flags & Pak.applicationInfo.FLAG_SYSTEM) < = 0) {//Add your own installed applications//Apps.add (PAK);} Apps.add (Pak);} return apps;} Private class Clicklistener implements Onitemclicklistener {@Overridepublic void Onitemclick (adapterview<?> arg0 , View arg1, int position,long arg3) {Intent Intent = new Intent (); Intent = PackageInfoDemo.this.getPackageManager (). Getla Unchintentforpackage (Datas.get (position) pakagename); startactivity (intent);}}
Using Resolveinfo
public class Resolveinfodemo extends Activity {private GridView gridview;private Packagemanager pmanager;private List <PakageMod> datas;private String tag = "Resolveinfodemo"; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {//TODO auto-generated method Stubsuper.oncreate (savedinstancestate); This.setcontentview ( R.layout.activity_main);//Get Gridviewgridview = (GridView) Findviewbyid (R.id.gridview);//Get picture, application name, package name Pmanager = This.getpackagemanager ();d atas = new Getallapps (this). Getdatas (); Gridview.setadapter (New Demogridadapter datas)); Gridview.setonitemclicklistener (new Clicklistener ());} When the user taps the application icon, the class responds with the private class Clicklistener implements Onitemclicklistener {@Overridepublic void Onitemclick ( Adapterview<?> Adapterview, view view, int arg2,long arg3) {Intent Intent = new Intent (); Intent = Getpackagemanager ( ). Getlaunchintentforpackage (Datas.get (arg2). Pakagename); startactivity (intent);}}
Getallapps.java
public class Getallapps {private Context mcontext;private packagemanager packagemanager;private int micondpi;private String tag = "Getallapps";p rivate list<pakagemod> datas = new arraylist<pakagemod> ();p ublic Getallapps ( Context mcontext) {this.mcontext = Mcontext; Activitymanager Activitymanager = (activitymanager) mcontext.getsystemservice (context.activity_service);p AC Kagemanager = Mcontext.getpackagemanager (); micondpi = Activitymanager.getlauncherlargeicondensity ();} public void Loadallappsbybatch () {list<resolveinfo> apps = null;intent mainintent = new Intent (Intent.action_main, NULL); Mainintent.addcategory (intent.category_launcher); apps = Packagemanager.queryintentactivities (mainIntent, 0) ; for (int i = 0; i < apps.size (); i++) {String PackageName = Apps.get (i). ActivityInfo.applicationInfo.packageName; String title = Apps.get (i). Loadlabel (Packagemanager). toString ();D rawable icon = null;if (title = = null) {title = Apps.get ( i). Activityinfo.name;} ActivitYinfo info = apps.get (i). Activityinfo;icon = Getfullresicon (info);d Atas.add (New Pakagemod (Packagename,title,icon));}} Public drawable Getfullresicon (activityinfo info) {Resources resources; try {resources = packagemanager.getresourcesforapplication (Info.applicationinfo); } catch (Packagemanager.namenotfoundexception e) {resources = null; } if (resources! = null) {int iconid = Info.geticonresource (); if (iconID! = 0) {return Getfullresicon (resources, iconID); }} return Getfullresdefaultactivityicon (); }public drawable Getfullresdefaultactivityicon () {return Getfullresicon (Resources.getsystem (), Android Oid. R.mipmap.sym_def_app_icon); }public drawable Getfullresicon (resources resources, int iconid) {drawable D; try {//requires API level (current min is): D = resources.gEtdrawablefordensity (iconID, micondpi); } catch (Resources.notfoundexception e) {d = null; } return (d! = null)? D:getfullresdefaultactivityicon (); }public list<pakagemod> Getdatas () {loadallappsbybatch (); return datas;}}
Here getdrawablefordensity need to be 15 version of the SDK support, all the lower version of the launcher can not be used;
Neither of the two methods used in this blog post requires any permissions to be configured;
Android4.2 launcher in the use of Resolveinfo to obtain the application list, where the Resolveinfo of the demo is also copied from the launcher source;
Maybe there's a 3rd way .... adjourned
This article is from the CSDN blog, reprint please contact the author;
Annotated source http://blog.csdn.net/dreamintheworld/article/details/39718581
Android Launcher app list for mobile phone installation