Learn how to get the mobile phone program list and program-related information and start the specified program
:
Program list:
Start the program and obtain the program information:
The Code is as follows:
Create an AppInfo class to represent the application
Public class AppInfo {public CharSequence title; // program name public CharSequence packageName; // package name Intent intent; // start Intent public Drawable icon; // program icon/** set Intent */final void setActivity (ComponentName className, int launchFlags) {intent = new Intent (Intent. ACTION_MAIN); intent. addCategory (Intent. CATEGORY_LAUNCHER); intent. setComponent (className); intent. setFlags (launchFlags );}}
Create an adapter for the program list:
/*** Program list adapter ** @ author bill **/public class ShowAppListAdapter extends BaseAdapter {private ArrayList appList; private LayoutInflater inflater; public ShowAppListAdapter (Context context, ArrayList 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 set */private ArrayList appInfos; private ListView lv_app;/** manage application packages, and obtain the program information */private PackageManager pm; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. app_list); pm = 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 ShowAppListAdapter (MainActivity. this, appInfos, pm) ;}};/*** load application list */private void loadApplications () {PackageManager manager = this. getPackageManager (); Intent mainIntent = new Intent (Intent. ACTION_MAIN, null); mainIntent. addCategory (Intent. CATEGORY_LAUNCHER); final List
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 (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; public void onItemClick (AdapterView
View, View arg1, final int position, long arg3) {AlertDialog. builder builder = new AlertDialog. builder (MainActivity. this); builder. setTitle (option); builder. setItems (R. array. choice, new OnClickListener () {public void onClick (DialogInterface dialog, int which) {final AppInfo appInfo = appInfos. get (position); switch (which) {case 0: // start the program try {startApp (appInfo);} catch (Exception e) {} break; case 1: // For 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, the program cannot be started, Toast. LENGTH_SHORT); return;} Intent intent = new Intent (); intent. setComponent (new ComponentName (packName, activityName); startActivity (intent) ;}}); dialog = builder. create (); dialog. show () ;}/ *** get program information * @ param packName * @ return * @ throws Exception */public PackageInfo getAppPackinfo (String packName) throws Exception {return pm. getPackageInfo (packName, PackageManager. GET_ACTIVITIES | PackageManager. GET_PERMISSIONS);}/*** get the Activity that starts the relevant program * @ param packName * @ return * @ throws Exception */public String getActivityName (String packName) throws 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:
App_list_item.xml:
/*** View application information ** @ author bill **/public class ShowAppDetailActivity extends Activity {private TextView TV _appname; private TextView 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 ();} TV _appname.setText (appname); TV _appversion.setText (appversion); TV _packagename.setText (packagename); TV _permission.setText (sb. toString ());}}