效果图:
程序列表:
启动程序,获取程序信息:
代码如下:
创建一个AppInfo类来表示应用程序
1. <pre name="code" class="java">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<AppInfo> appList;
8. private LayoutInflater inflater;
9.
10. public ShowAppListAdapter(Context context,ArrayList<AppInfo> 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<AppInfo> 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<ResolveInfo> 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<AppInfo>(count);
61. }
62. appInfos.clear();
63. for (int i = 0; i < count; i++) {
64. new AppInfo();
65. ResolveInfo info = apps.get(i);
66. application.title = info.loadLabel(manager);
67. new ComponentName(
68. info.activityInfo.applicationInfo.packageName,
69. info.activityInfo.name), Intent.FLAG_ACTIVITY_NEW_TASK
70. | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
71. application.icon = info.activityInfo.loadIcon(manager);
72. application.packageName = info.activityInfo.applicationInfo.packageName;
73. appInfos.add(application);
74. }
75. }
76. }
77.
78.
79. /**
80. * 列表监听类
81. * @author bill
82. *
83. */
84. public final class AppDetailLinster implements OnItemClickListener {
85.
86. AlertDialog dialog;
87.
88. public void onItemClick(AdapterView<?> 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 <= 0) {
179. return null;
180. }
181. return activitys[0].name;
182. }
183. }
app_list.xml:
1. <?xml version="1.0" encoding="utf-8"?>
2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
3. android:layout_width="fill_parent"
4. android:layout_height="fill_parent"
5. android:orientation="vertical"
6. android:background="@android:color/black" >
7.
8.
9.
10. <ListView
11. android:id="@+id/app_list_view"
12. android:layout_width="fill_parent"
13. android:layout_height="fill_parent"
14. >
15. </ListView>
16.
17. </RelativeLayout>
app_list_item.xml:
2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3. android:orientation="horizontal" android:layout_width="fill_parent"
4. android:layout_height="wrap_content" android:gravity="center_vertical">
5.
6. <ImageView
7. android:id="@+id/lv_icon"
8. android:layout_width="48px"
9. android:layout_height="48px"
10. android:layout_marginTop="5px"
11. android:layout_marginBottom="5px"
12. ></ImageView>
13. <LinearLayout
14. android:orientation="vertical"
15. android:layout_width="wrap_content"
16. android:layout_height="48px"
17. android:paddingLeft="5px"
18. >
19. <TextView
20. android:id="@+id/lv_item_appname"
21. android:layout_width="fill_parent"
22. android:layout_height="wrap_content"
23. android:singleLine="true"
24. android:textSize="16px"
25. android:textStyle="bold"
26. android:textColor="#fff"
27. ></TextView>
28.
29. <TextView
30. android:id="@+id/lv_item_packageame"
31. android:layout_width="fill_parent"
32. android:layout_height="wrap_content"
33. android:singleLine="true"
34. android:textColor="#fff"
35. ></TextView>
36.
37.
38. </LinearLayout>
39. </LinearLayout>
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. <?xml version="1.0" encoding="utf-8"?>
2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3. android:orientation="vertical"
4. android:layout_width="fill_parent"
5. android:layout_height="fill_parent"
6. >
7. <TableLayout
8. android:id="@+id/app_table"
9. android:layout_width="fill_parent"
10. android:layout_height="wrap_content">
11. <TableRow
12. android:id="@+id/tableRow1"
13. android:layout_width="wrap_content"
14. android:layout_height="wrap_content"
15. >
16. <TextView
17. android:layout_width="wrap_content"
18. android:layout_height="wrap_content"
19. android:text="程序名字"
20. />
21. <TextView
22. android:layout_width="wrap_content"
23. android:layout_height="wrap_content"
24. android:id="@+id/detail_app_name"
25. />
26. </TableRow>
27.
28. <TableRow
29. android:id="@+id/tableRow2"
30. android:layout_width="wrap_content"
31. android:layout_height="wrap_content">
32. <TextView
33. android:layout_width="wrap_content"
34. android:layout_height="wrap_content"
35. android:text="程序版本"
36. />
37. <TextView
38. android:layout_width="wrap_content"
39. android:layout_height="wrap_content"
40. android:id="@+id/detail_app_version"
41. />
42. </TableRow>
43. <TableRow
44. android:id="@+id/tableRow3"
45. android:layout_width="wrap_content"
46. android:layout_height="wrap_content">
47. <TextView
48. android:layout_width="wrap_content"
49. android:layout_height="wrap_content"
50. android:text="程序包名"
51. />
52. <TextView
53. android:layout_width="wrap_content"
54. android:layout_height="wrap_content"
55. android:id="@+id/detail_app_packname"
56. />
57. </TableRow>
58. <TableRow
59. android:id="@+id/tableRow4"
60. android:layout_width="wrap_content"
61. android:layout_height="wrap_content">
62. <TextView
63. android:layout_width="wrap_content"
64. android:layout_height="wrap_content"
65. android:text="程序权限"
66. />
67. <TextView
68. android:layout_width="wrap_content"
69. android:layout_height="wrap_content"
70. android:id="@+id/detail_app_permissions"
71. />
72. </TableRow>
73. </TableLayout>
74.
75. </LinearLayout>
最后别忘了配置
AndroidManifest。