Obtain all software information for Android Development
Program running:
Program code:
/*** Obtain all software information ** 1. display All software in the system in asynchronous mode * 2. click Open specified software * 3. local SharedPreferences * @ author jph * Date: 2014.09.21 */public class ScanPackage1 extends Activity {/** scan successful **/private final static int FLAG_LOAD_SUCCESS = 0x10001; private final static int SCANNING = 0x10002; private ListView list; private List
> Items = new ArrayList
> (); Private SimpleAdapter adapter; // obtain all installed software information. private List
AllPackageInfos; // obtain the software information you have installed. private List
UserPackageInfos; // obtain the software information installed by the system. private List
SysPackageInfos; Handler mHandler = new Handler () {@ Overridepublic void handleMessage (Message msg) {// TODO Auto-generated method stubswitch (msg. what) {case FLAG_LOAD_SUCCESS: // scan break; case SCANNING: // SCANNING items. add (Map
) Msg. obj); // notify the adapter to change the adapter data. yydatasetchanged (); break; default: break ;}};@ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. sp_layout); list = (ListView) findViewById (R. id. list); new ScanThread (). start (); adapter = new SimpleAdapter (this, items, R. layout. line, new String [] {"imgIco", "appName", "packageName"}, new int [] {R. id. imgIco, R. id. tvAppName, R. id. tvAppDesc}); list. setAdapter (adapter); // The ViewBinder class can help SimpleAdapter load the image (for example, Bitmap, Drawable) adapter. setViewBinder (new ViewBinder () {@ Overridepublic boolean setViewValue (View view, Object data, String textRepresentation) {// TODO Auto-generated method stub if (view instanceof ImageView & data instanceof Drawable) {ImageView iv = (ImageView) view; iv. setImageDrawable (Drawable) data); return true ;}else {return false ;}}); list. setOnItemClickListener (new OnItemClickListener () {@ Overridepublic void onItemClick (AdapterView
Arg0, View arg1, int arg2, long arg3) {// TODO Auto-generated method stubtry {PackageInfo pInfo = allPackageInfos. get (arg2); Intent intent = new Intent (); intent. setComponent (new ComponentName (pInfo. packageName, pInfo. activities [0]. name); startActivity (intent);} catch (Exception e) {// TODO: handle has tione. printStackTrace ();}}});} // *************** -------- * Create a thread to load the installer *--------------************* * ***** // private class ScanThread extends Thread {@ Overridepublic void run () {// obtain information about all software installed in the system. allPackageInfos = getPackageManager (). getInstalledPackages (PackageManager. GET_UNINSTALLED_PACKAGES | PackageManager. GET_ACTIVITIES); // defines the user's Installation Software Package userPackageInfos = new ArrayList
(); // Define the system installation software package sysPackageInfos = new ArrayList
(); // Cyclically retrieve all software information for (int I = 0; I <allPackageInfos. size (); I ++) {// obtain each software information PackageInfo temp = allPackageInfos. get (I); ApplicationInfo appInfo = temp. applicationInfo; if (appInfo. flags & ApplicationInfo. FLAG_UPDATED_SYSTEM_APP )! = 0 | (appInfo. flags & ApplicationInfo. FLAG_SYSTEM )! = 0) {// System Software sysPackageInfos. add (temp);} else {// The user installs the software userPackageInfos. add (temp);} // obtain the program icon Drawable ico = ScanPackage1.this. getPackageManager (). getApplicationIcon (appInfo); // obtain the program name String appName = (String) ScanPackage1.this. getPackageManager (). getApplicationLabel (appInfo); Map
Item = new HashMap
(); // Get the package name of the program String packageName = appInfo. packageName; item. put ("imgIco", ico); item. put ("appName", appName); item. put ("packageName", packageName); Message message = new Message (); message. what = SCANNING; message. obj = item; mHandler. sendMessage (message);} saveInfo (sysPackageInfos, userPackageInfos); mHandler. sendEmptyMessage (FLAG_LOAD_SUCCESS );}}; /*** write information about programs installed in the system to the configuration file * @ param sysPackageInfos System Installation Software Package * @ param userPackageInfos user installation software package */private void saveInfo (List
SysPackageInfos, List
UserPackageInfos) {// Add the software installed by the user to the sysPackageInfos collection added to the system software. addAll (userPackageInfos); SharedPreferences sp = this. getSharedPreferences ("appInfs", MODE_PRIVATE); Editor editor = sp. edit (); for (int I = 0; I <sysPackageInfos. size (); I ++) {try {// get the package name of the program String packageName = sysPackageInfos. get (I ). packageName; // retrieves activity information ActivityInfo activityInfo = sysPackageInfos. get (I ). activities [0]; // retrieve activity name String activityName = activityInfo. name; // write program information to the configuration file editor. putString (packageName, activityName);} catch (Exception e) {// TODO: handle effectione. printStackTrace () ;}} editor. commit ();}}