Android development obtains all installed programs on the local machine (forming ListView), Android listview

Source: Internet
Author: User

Android development obtains all installed programs on the local machine (forming ListView), Android listview

This is exactly what the project needs. After querying and summarizing a lot of information, it finally solved the problem smoothly and accumulated a lot of data query skills. Now I want to sort out my ideas, I hope to be helpful to those who want to develop similar functions and make friends who love mobile development. Next, let's get started!

 

A bad habit of writing code by myself is that the decoupling between classes is very poor, and a bunch of code is piled up together, never considering the planning interface and tool class.


Create a class to save all the information of an APP, including the name, icon, and version number.

// 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; // supplier private String InstalPath; // installation path private int flag; // determines whether the system application is public int getVersionCode () {return versionCode;} public void setAppicon (PackageManager packageManager) {// TODO Auto-generated method stub} public void setVersionCode (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) {this. provider = providers;}/*** @ return the instalPath */public String getInstalPath () {return InstalPath ;} /*** @ param instalPath the instalPath to set */public void setInstalPath (String instalPath) {InstalPath = instalPath;} public void setFlags (int flag) {this. flag = flag;} public int getFlags () {return this. flag ;}}

Before writing, this class seems to have written a lot of unnecessary things, but the robustness of the program is greatly increased. If you need to develop the APP list in depth in the future, you can add a setter and a getter to get the code logic at a glance. Continue.

Create a Get function and return a set to save all the content to be queried.


// Get function private ArrayList <AllAppInfo> Get () {ArrayList <AllAppInfo> appList = new ArrayList <AllAppInfo> (); 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. setPackagename (pInfo. packageName); // application package allAppInfo. setVersionCode (pInfo. versionCode); // version allAppInfo. setFlags (pInfo. applicationInfo. flags); // uid 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 completing the above two steps, the entire module is basically complete, and 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. simpl EAdapter; import android. widget. simpleAdapter. viewBinder; public class AboutMore extends Activity {private ListView listview; private List <Map <String, Object> listitem; private String [] systemApp = {"set", "recorder ", "Clock"};/** Called when the activity is first created. * // @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. aboutmore_lay Out); 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. getInstalPa Th (); if (allAppInfo. getFlags () & ApplicationInfo. FLAG_SYSTEM)> 0) // if it is determined to be a system application {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. getInstal Path (); 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 I Nt [] {R. id. logo, R. id. app_name}); myAdapter. setViewBinder (new ViewBinder () {public 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); // instantiate listviewlistview. setAda Pter (myAdapter); // Add an adapter to the listview // The event listening listview. setOnItemClickListener (new OnItemClickListener () {@ Overridepublic void onItemClick (AdapterView <?> Parent, View view, int position, long id) {// TODO Auto-generated method stub String packagename = listitem. get (position ). get ("packagename "). toString (); doStartApplicationWithPackageName (packagename );}});}}

At the end of the above Code, a method is called to enable the call through the program package. The following code

Related Article

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.