Learn how to get the mobile phone program list and program-related information and start the specified program (26), android-related information

Source: Internet
Author: User

Learn how to get the mobile phone program list and program-related information and start the specified program (26), android-related information

:

Program list:


Start the program and obtain the program information:







The Code is as follows:

Create an AppInfo class to represent the application

<Pre name = "code" class = "java"> public class AppInfo {public CharSequence title; // program name public CharSequence packageName; // program name Intent intent; // start Intentpublic 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 <AppInfo> appList; private LayoutInflater inflater; public ShowAppListAdapter (Context context, arrayList <AppInfo> 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 <AppInfo> 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 = (Li StView) 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 the Application List */private void loadApplications () {PackageManager manager = this. getPackageManager (); Intent mainIntent = new Intent (Intent. ACTION_MAIN, null); mainIntent. addCategory (Intent. CATEGORY_LAUNCHER); final List <ResolveInfo> 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 <AppInfo> (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_NE W_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:

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical"    android:background="@android:color/black" >    <ListView        android:id="@+id/app_list_view"        android:layout_width="fill_parent"        android:layout_height="fill_parent"      >    </ListView></RelativeLayout>



App_list_item.xml:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="horizontal" android:layout_width="fill_parent"android:layout_height="wrap_content" android:gravity="center_vertical"><ImageViewandroid:id="@+id/lv_icon"android:layout_width="48px"android:layout_height="48px"android:layout_marginTop="5px"android:layout_marginBottom="5px"></ImageView><LinearLayoutandroid:orientation="vertical"android:layout_width="wrap_content"android:layout_height="48px"android:paddingLeft="5px"><TextViewandroid:id="@+id/lv_item_appname"android:layout_width="fill_parent"android:layout_height="wrap_content"android:singleLine="true"android:textSize="16px"android:textStyle="bold"android:textColor="#fff"></TextView><TextViewandroid:id="@+id/lv_item_packageame"android:layout_width="fill_parent"android:layout_height="wrap_content"android:singleLine="true"android:textColor="#fff"></TextView></LinearLayout></LinearLayout>



/*** 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 ("\ n");} TV _appname.setText (appname); TV _appversion.setText (appversion); TV _packagename.setText (packagename); TV _permission.setText (sb. toString ());}}

App_detial.xml:

<? Xml version = "1.0" encoding = "UTF-8"?> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: orientation = "vertical" android: layout_width = "fill_parent" android: layout_height = "fill_parent"> <TableLayout android: id = "@ + id/app_table" android: layout_width = "fill_parent" android: layout_height = "wrap_content"> <TableRow android: id = "@ + id/tableRow1" android: layout_width = "wrap_content" android: layout_height = "wrap_content"> <TextView android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "program name"/> <TextView android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: id = "@ + id/detail_app_name"/> </TableRow> <TableRow android: id = "@ + id/tableRow2" android: layout_width = "wrap_content" android: layout_height = "wrap_content"> <TextView android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "program version"/> <TextView android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: id = "@ + id/detail_app_version"/> </TableRow> <TableRow android: id = "@ + id/tableRow3" android: layout_width = "wrap_content" android: layout_height = "wrap_content"> <TextView android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "package name"/> <TextView android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: id = "@ + id/detail_app_packname"/> </TableRow> <TableRow android: id = "@ + id/tableRow4" android: layout_width = "wrap_content" android: layout_height = "wrap_content"> <TextView android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "app permission"/> <TextView android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: id = "@ + id/detail_app_permissions"/> </TableRow> </TableLayout> </LinearLayout>

Finally, do not forget to configure AndroidManifest.



Reprinted please indicate the source: http://blog.csdn.net/hai_qing_xu_kong/article/details/44274387 sentiment control _

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.