一起学android之如何获取手机程序列表以及程序相关信息并启动指定程序 (26)

2023年3月28日

效果图:

程序列表:

启动程序,获取程序信息:

代码如下:

创建一个AppInfo类来表示应用程序

1. 
public class AppInfo {  
2. public CharSequence title;// 程序名  
3. public CharSequence packageName; // 程序包名  
4. // 启动Intent  
5. public Drawable icon;// 程序图标  
6.   
7. /*
8.      * 设置启动该程序的Intent
9.      */  
10. final void setActivity(ComponentName className, int launchFlags) {  
11. new Intent(Intent.ACTION_MAIN);  
12.         intent.addCategory(Intent.CATEGORY_LAUNCHER);  
13.         intent.setComponent(className);  
14.         intent.setFlags(launchFlags);  
15.     }  
16.   
17. }

创建程序列表的适配器:

1. /**
2.  * 程序列表适配器
3.  * @author bill
4.  *
5.  */  
6. public class ShowAppListAdapter extends BaseAdapter {  
7. private ArrayList appList;  
8. private LayoutInflater inflater;  
9.       
10. public ShowAppListAdapter(Context context,ArrayList appList,  
11.             PackageManager pm) {  
12. this.appList = appList;  
13.         inflater = LayoutInflater.from(context);  
14.     }  
15.   
16. public int getCount() {  
17. return appList.size();  
18.     }  
19.   
20.   
21. public Object getItem(int position) {  
22. return appList.get(position);  
23.     }  
24.   
25.   
26. public long getItemId(int position) {  
27. return position;  
28.     }  
29.   
30.   
31. public View getView(int position, View convertView, ViewGroup parent) {  
32. final AppInfo info = appList.get(position);  
33. null;  
34. if(null == convertView){  
35. null);  
36. new ViewHolder();  
37.             holder.lv_image = (ImageView) convertView.findViewById(R.id.lv_icon);  
38.             holder.lv_name = (TextView) convertView.findViewById(R.id.lv_item_appname);  
39.             holder.lv_packname = (TextView) convertView.findViewById(R.id.lv_item_packageame);  
40.             convertView.setTag(holder);  
41.         }  
42. else {  
43.             holder = (ViewHolder) convertView.getTag();  
44.         }  
45.         holder.lv_image.setImageDrawable(info.icon);  
46. final CharSequence name = info.title;  
47. final CharSequence packName = info.packageName;  
48.         holder.lv_name.setText(name);  
49.         holder.lv_packname.setText(packName);  
50. return convertView;  
51.     }  
52. private final static  class ViewHolder{  
53.         ImageView lv_image;  
54.          TextView lv_name;  
55.          TextView lv_packname;  
56.     }  
57.   
58.       
59. }
1. public class MainActivity extends Activity {  
2. /*
3.      * 应用程序集合
4.      */  
5. private ArrayList appInfos;  
6. private ListView lv_app;  
7. /*
8.      * 管理应用程序包,并通过它获取程序信息
9.      */  
10. private PackageManager pm;  
11. @Override  
12. protected void onCreate(Bundle savedInstanceState) {  
13. super.onCreate(savedInstanceState);  
14.         setContentView(R.layout.app_list);  
15.         pm = getPackageManager();  
16.         initView();  
17. new Thread(runable).start();  
18.     }  
19.   
20. private void initView(){  
21.         lv_app = (ListView) findViewById(R.id.app_list_view);  
22. new AppDetailLinster());  
23.     }  
24.   
25.       
26. private final Runnable runable = new Runnable() {  
27.   
28. public void run() {  
29.             loadApplications();  
30.             myHandler.obtainMessage().sendToTarget();  
31.         }  
32.   
33.     };  
34.   
35. private Handler myHandler = new Handler() {  
36.   
37. @Override  
38. public void handleMessage(Message msg) {  
39. new ShowAppListAdapter(MainActivity.this,  
40.                     appInfos, pm));  
41.               
42.               
43.         }  
44.   
45.     };  
46.       
47. /**
48.      * 加载应用列表
49.      */  
50. private void loadApplications() {  
51. this.getPackageManager();  
52. new Intent(Intent.ACTION_MAIN, null);  
53.         mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);  
54. final List apps = manager.queryIntentActivities(  
55. 0);  
56. new ResolveInfo.DisplayNameComparator(manager));  
57. if (apps != null) {  
58. final int count = apps.size();  
59. if (appInfos == null) {  
60. new ArrayList(count);  
61.             }  
62.             appInfos.clear();  
63. for (int i = 0; i  view, View arg1,  
89. final int position, long arg3) {  
90. new AlertDialog.Builder(  
91. this);  
92. "选项");  
93. new OnClickListener() {  
94. public void onClick(DialogInterface dialog, int which) {  
95. final AppInfo appInfo = appInfos.get(position);  
96. switch (which) {  
97. case 0: // 启动程序  
98. try {  
99.                             startApp(appInfo);  
100. catch (Exception e) {  
101.                               
102.                         }  
103. break;  
104. case 1: // 详细信息  
105. try {  
106.                             showAppDetail(appInfo);  
107. catch (Exception e) {  
108.                               
109.                         }  
110. break;  
111.                   
112.                     }  
113.                     dialog.dismiss();  
114.                 }  
115.   
116. private void showAppDetail(AppInfo appInfo)  
117. throws Exception {  
118. final String packName = appInfo.packageName.toString();  
119. final PackageInfo packInfo = getAppPackinfo(packName);  
120. final String versionName = packInfo.versionName;  
121. final String[] apppremissions = packInfo.requestedPermissions;  
122. final String appName = appInfo.title.toString();  
123. new Intent(MainActivity.this,  
124. class);  
125. new Bundle();  
126. "packagename", packName);  
127. "appversion", versionName);  
128. "apppremissions", apppremissions);  
129. "appname", appName);  
130.                     showDetailIntent.putExtras(bundle);  
131.                     startActivity(showDetailIntent);  
132.   
133.                 }  
134.   
135. private void startApp(AppInfo appInfo)  
136. throws Exception {  
137. final String packName = appInfo.packageName.toString();  
138. final String activityName = getActivityName(packName);  
139. if (null == activityName) {  
140. this, "程序无法启动",  
141.                                 Toast.LENGTH_SHORT);  
142. return;  
143.                     }  
144. new Intent();  
145. new ComponentName(packName,  
146.                             activityName));  
147.                     startActivity(intent);  
148.                 }  
149.   
150.             });  
151.             dialog = builder.create();  
152.             dialog.show();  
153.   
154.         }  
155.   
156.     }  
157. /**
158.      * 获取程序信息
159.      * @param packName
160.      * @return
161.      * @throws Exception
162.      */  
163. public PackageInfo getAppPackinfo(String packName) throws Exception {  
164. return pm.getPackageInfo(packName, PackageManager.GET_ACTIVITIES  
165.                 | PackageManager.GET_PERMISSIONS);  
166.     }  
167.       
168. /**
169.      * 获取启动相关程序的Activity
170.      * @param packName
171.      * @return
172.      * @throws Exception
173.      */  
174. public String getActivityName(String packName) throws Exception {  
175. final PackageInfo packInfo = pm.getPackageInfo(packName,  
176.                 PackageManager.GET_ACTIVITIES);  
177. final ActivityInfo[] activitys = packInfo.activities;  
178. if (null == activitys || activitys.length 

app_list.xml:

1.   
2.   
7.   
8.   
9.   
10.   
15.   
16.   
17. 

app_list_item.xml:

2.   
5.       
6.   
13.   
19.   
28.           
29.   
36.           
37.           
38.   
39. 

1. /**
2.  * 查看应用信息
3.  * @author bill
4.  *
5.  */  
6. public class ShowAppDetailActivity extends Activity {  
7.   
8. private TextView tv_appname;  
9. private TextView tv_appversion;  
10. private TextView tv_packagename;  
11. private TextView tv_permission;  
12. @Override  
13. protected void onCreate(Bundle savedInstanceState) {  
14. // TODO Auto-generated method stub  
15. super.onCreate(savedInstanceState);  
16.         setContentView(R.layout.app_detial);  
17.         tv_appname = (TextView) findViewById(R.id.detail_app_name);  
18.         tv_appversion = (TextView) findViewById(R.id.detail_app_version);  
19.         tv_packagename = (TextView) findViewById(R.id.detail_app_packname);  
20.         tv_permission = (TextView) findViewById(R.id.detail_app_permissions);  
21. this.getIntent().getExtras();  
22. "packagename");  
23. "appversion");  
24. "appname");  
25. "apppremissions");  
26. new StringBuilder();  
27. for(String s : appPremissions){  
28.             sb.append(s);  
29. "n");  
30.         }  
31.         tv_appname.setText(appname);  
32.         tv_appversion.setText(appversion);  
33.         tv_packagename.setText(packagename);  
34.         tv_permission.setText(sb.toString());  
35.     }  
36. }

app_detial.xml:

1.   
2.   
7.   
11.   
16.   
21.   
26.   
27.            
28.   
32.   
37.   
42.   
43.   
47.   
52.   
57.   
58.   
62.   
67.       
72.   
73.   
74.   
75. 

最后别忘了配置

AndroidManifest。

服务器托管,北京服务器托管,服务器租用 http://www.hhisp.net
机房租用,北京机房租用,IDC机房托管, http://www.e1idc.net

hackdl

咨询热线/微信 13051898268