Android Development Get this machine all installed programs (Form ListView)

Source: Internet
Author: User

The project just need to use this, the query summarizes a lot of data finally successfully solved, but also accumulated a lot of information to check the skills, now the idea of sorting out, hoping to do similar functions of friends have to help, hope to make love mobile development friends. Let's cut to the chase!

Before you write your own code a very bad habit is the class of mutual decoupling between the poor, a bunch of code are stacked together, never consider the planning interface, tool class.


This first creates a class that holds all the information for an app, including the name, icon, version number, and so on.

Allappinfo class Allappinfo {private int versioncode = 0;    Version number private string appname = "";//program name private string packagename = ""; Package name private drawable AppIcon;//icon private long lastinstal;//Last installation time private providerinfo[] provider;  Vendor Private String instalpath;//installation path private int flag;    Determine if the system is applying public int getversioncode () {return versioncode; } public void Setappicon (Packagemanager packagemanager) {//TODO auto-generated method stub}public void Setversionc    Ode (int versioncode) {this.versioncode = Versioncode;    } public String Getappname () {return appname;    } public void Setappname (String appname) {this.appname = appname;    } public String Getpackagename () {return packagename;    } public void Setpackagename (String packagename) {this.packagename = PackageName; } public drawable Getappicon () {return APPicon    } public void Setappicon (drawable appicon) {This.appicon = AppIcon;    }/** * @return the lastinstal * * * public long getlastinstal () {return lastinstal;        }/** * @param firstinstalltime the lastinstal to set */public void setlastinstal (long firstinstalltime) {    This.lastinstal = Firstinstalltime;    }/** * @return the provider * */public providerinfo[] Getprovider () {return provider; }/** * @param providers the provider to set */public void Setprovider (providerinfo[] providers) {thi    S.provider = providers;    }/** * @return the Instalpath * */Public String Getinstalpath () {return instalpath; }/** * @param instalpath the Instalpath to set */public void Setinstalpath (String instalpath) {Insta    Lpath = Instalpath;  } public void SetFlags (int flag) {This.flag = flag;  } public int GetFlags () {return this.flag;   }}

Before writing this class seems to write a lot of superfluous things, but the robustness of the program greatly increased, in the future need to further develop the app list, a setter plus a getter directly, code logic at a glance. Go on.

Here you continue to create a get function that returns a collection that holds all the content that needs to be queried


Get function Private arraylist<allappinfo> get () {arraylist<allappinfo> applist = new Arraylist<allappin         Fo> ();      List<packageinfo> Packageinfos=getpackagemanager (). getinstalledpackages (0);          for (int i = 0; i < packageinfos.size (); i++) {PackageInfo pinfo=packageinfos.get (i);          Allappinfo allappinfo=new allappinfo (); Allappinfo.setappname (PInfo.applicationInfo.loadLabel (Getpackagemanager ()). ToString ());//The name of the application Allappinfo.set PackageName (pinfo.packagename);//Application Package Allappinfo.setversioncode (Pinfo.versioncode);//version number allappinfo.   SetFlags (PInfo.applicationInfo.flags);          UID number allappinfo.setlastinstal (pinfo.firstinstalltime);           Allappinfo.setprovider (pinfo.providers);        Allappinfo.setappicon (PInfo.applicationInfo.loadIcon (Getpackagemanager ()));          Allappinfo.setinstalpath (PInfo.applicationInfo.sourceDir);      Applist.add (Allappinfo); } return applist;     }       

After the above two steps are finished, the whole module is basically, the rest is to configure the interface adapter.


Import Java.util.arraylist;import java.util.arrays;import Java.util.hashmap;import Java.util.iterator;import Java.util.list;import Java.util.map;import Android.app.activity;import Android.content.componentname;import Android.content.intent;import Android.content.pm.applicationinfo;import Android.content.pm.packageinfo;import Android.content.pm.packagemanager;import Android.content.pm.packagemanager.namenotfoundexception;import Android.content.pm.providerinfo;import Android.content.pm.resolveinfo;import android.graphics.drawable.Drawable; Import Android.os.bundle;import android.view.view;import android.widget.adapter;import Android.widget.AdapterView; Import Android.widget.adapterview.onitemclicklistener;import Android.widget.imageview;import Android.widget.listview;import Android.widget.simpleadapter;import Android.widget.SimpleAdapter.ViewBinder; public class Aboutmore extends Activity {private listview listview;p rivate list<map<string, object>> listit EM;p rivate string[]Systemapp = {"Settings", "Sound Recorder", "clock"};/** called when the activity is first created.          */@Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);   Setcontentview (r.layout.aboutmore_layout);      ListItem = new arraylist<map<string,object>> ();           Get (); Arraylist<allappinfo> List=this.          Get ();         for (iterator<allappinfo> Iterator = List.iterator (); Iterator.hasnext ();)              {Allappinfo Allappinfo = (allappinfo) iterator.next (); System.out.println (Allappinfo.getappname () + "|" +allappinfo.getpackagename () + "| "+allappinfo.getversioncode () +" | " +allappinfo.getlastinstal () + "|"              +allappinfo.getinstalpath ());            if ((Allappinfo.getflags () & Applicationinfo.flag_system) >0)//If the system is determined to apply {int FLAG = 0; for (int i = 0; i<systemapp.length; i++) {if (Systemapp[i].equals (Allappinfo.getappname (   )) )         {flag = 1;                        }} if (flag = = 0) continue; }//System.out.println (Allappinfo.getappname () + "|" +allappinfo.getpackagename () + "| "+allappinfo.getversioncode () +" | " +allappinfo.getlastinstal () + "|"              +allappinfo.getinstalpath ());            map<string, object> showitem = new hashmap<string, object> ();              Showitem.put ("logo", Allappinfo.getappicon ());              Showitem.put ("AppName", Allappinfo.getappname ());            Showitem.put ("PackageName", Allappinfo.getpackagename ());          Listitem.add (ShowItem); } simpleadapter Myadapter = new Simpleadapter (Getapplicationcontext (), ListItem, R.layout.aboutmore_item_layout, New string[]{"logo", "AppName"}, New Int[]{r.id.logo,r.id.app_name}); Myadapter.setviewbinder (new Viewbinder () {p Ublic boolean setviewvalue (view View,object data,string textrepresentation) {if (View Instanceof ImageView && Data instanceof drawable) {ImageView iv= (ImageView) view;                     Iv.setimagedrawable ((drawable) data);                   return true;            } else return false;    }}); listview = (ListView) Findviewbyid (R.id.listview);                           Instantiation of Listviewlistview.setadapter (Myadapter); Add an adapter for the ListView//Start adding a list below the event listener Listview.setonitemclicklistener (new Onitemclicklistener () {@Overridepublic void Onitemclick (adapterview<?> parent, View view,int position, long ID) {//TODO auto-generated method stub String PA    Ckagename = Listitem.get (position). Get ("PackageName"). ToString ();d ostartapplicationwithpackagename (PackageName);}}); }   }

Here at the end of the code above called a method, through the program's package to open the call, the following code to go from a blog. Blog Address http://blog.csdn.net/mad1989/article/details/38090513, writing is very good.


* * Call other programs by the name of the package * from: http://blog.csdn.net/mad1989/article/details/38090513 */private void dostartapplicationw Ithpackagename (String packagename) {//Get this app details through the package name, including activities, services, Versioncode, name, and more PackageInfo PackageInfo = null;try {packageinfo = Getpackagemanager (). Getpackageinfo (PackageName, 0);} catch ( Namenotfoundexception e) {e.printstacktrace ();} if (PackageInfo = = null) {return;} Create a class Category_launcher for the package name intentintent resolveintent = new Intent (intent.action_main, NULL); Resolveintent.addcategory (Intent.category_launcher); Resolveintent.setpackage (packageinfo.packagename);// list<resolveinfo> resolveinfolist = GetPackageManager () is traversed by the Queryintentactivities method of the Getpackagemanager (). Queryintentactivities (resolveintent, 0); ResolveInfo ResolveInfo = Resolveinfolist.iterator (). Next (); if (ResolveInfo! = null) {//packagename = parameter packnamestring p Ackagename = resolveinfo.activityinfo.packagename;//This is the launcher of the app we're looking for activity[ Organization form: Packagename.mainactivityname]sTring ClassName = resolveinfo.activityinfo.name;//LAUNCHER intentintent intent = new Intent (Intent.action_main); Intent.addcategory (Intent.category_launcher);//Set componentname parameter 1:packagename parameter 2:mainactivity path componentname CN = new ComponentName (PackageName, className); Intent.setcomponent (CN); StartActivity (intent);}


Finally get a picture of the sun. PS: I spend a lot of time on the code that gets the app icon, and I'll give you a special explanation. as follows, the completion of the Setappicon method of the Allappinfo class can be successfully removed when needed.

Allappinfo.setappicon (PInfo.applicationInfo.loadIcon (Getpackagemanager ()));  by Getpackagemanager



Android Development Get this machine all installed programs (Form ListView)

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.