在此我写一个找到android手机里的所有已装应用程序(android手机自带的也行),点击某个应用时,将做出响应 。
先看一下效果图:
当点击《愤怒的小鸟》时,将打开《愤怒的小鸟》这个游戏
main.xml的源代码如下:
view plain
copy to clipboard
print
?
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. <GridView xmlns:android="http://schemas.android.com/apk/res/android"
8. android:id="@+id/gridview"
9. android:layout_width="fill_parent"
10. android:layout_height="fill_parent"
11. android:numColumns="auto_fit"
12. android:verticalSpacing="15dp"
13. android:horizontalSpacing="12dp"
14. android:columnWidth="95dp"
15. android:stretchMode="columnWidth"
16. android:gravity="center"
17. />
18. </LinearLayout>
<?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"
>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="auto_fit"
android:verticalSpacing="15dp"
android:horizontalSpacing="12dp"
android:columnWidth="95dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>
</LinearLayout>
接着是apps.xml,源代码如下:
view plain
copy to clipboard
print
?
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. android:gravity="center" >
7.
8. <ImageView
9. android:id="@+id/apps_image"
10. android:layout_width="48dip"
11. android:layout_height="48dip"
12. android:icon="@drawable/icon" />
13.
14. <TextView
15. android:id="@+id/apps_textview"
16. android:layout_width="wrap_content"
17. android:layout_height="wrap_content"
18. android:maxLines="1"
19. android:maxEms="5"
20. android:text="good" />
21.
22. </LinearLayout>
<?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"
android:gravity="center" >
<ImageView
android:id="@+id/apps_image"
android:layout_width="48dip"
android:layout_height="48dip"
android:icon="@drawable/icon" />
<TextView
android:id="@+id/apps_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="1"
android:maxEms="5"
android:text="good" />
</LinearLayout>
接下来就是java源代码了,比较长,希望耐心看下去
view plain copy to clipboard print ?
1. package
2.
3. import
4. import
5.
6. import
7. import
8. import
9. import
10. import
11. import
12. import
13. import
14. import
15. import
16. import
17. import
18. import
19. import
20. import
21. import
22.
23. public class Apps extends
24.
25. // 用来记录应用程序的信息
26. List<AppsItemInfo> list;
27.
28. private
29. private
30.
31. @Override
32. protected void
33. // TODO Auto-generated method stub
34. super.onCreate(savedInstanceState);
35. this.setContentView(R.layout.main);
36.
37. // 取得gridview
38. gridview = (GridView) findViewById(R.id.gridview);
39.
40. // 获取图片、应用名、包名
41. this.getPackageManager();
42. this);
43.
44. new
45.
46. for (int i = 0; i < appList.size(); i++) {
47. PackageInfo pinfo = appList.get(i);
48. new
49. // 设置图片
50. shareItem.setIcon(pManager
51. .getApplicationIcon(pinfo.applicationInfo));
52. // 设置应用程序名字
53. shareItem.setLabel(pManager.getApplicationLabel(
54. pinfo.applicationInfo).toString());
55. // 设置应用程序的包名
56. shareItem.setPackageName(pinfo.applicationInfo.packageName);
57.
58. list.add(shareItem);
59.
60. }
61.
62. // 设置gridview的Adapter
63. new
64.
65. // 点击应用图标时,做出响应
66. new
67.
68. }
69.
70. public static
71.
72. new
73. PackageManager pManager = context.getPackageManager();
74. // 获取手机内所有应用
75. 0);
76. for (int i = 0; i < packlist.size(); i++) {
77. PackageInfo pak = (PackageInfo) packlist.get(i);
78.
79. // 判断是否为非系统预装的应用程序
80. // 这里还可以添加系统自带的,这里就先不添加了,如果有需要可以自己添加
81. // if()里的值如果<=0则为自己装的程序,否则为系统工程自带
82. if ((pak.applicationInfo.flags & pak.applicationInfo.FLAG_SYSTEM) <= 0) {
83. // 添加自己已经安装的应用程序
84. apps.add(pak);
85. }
86.
87. }
88. return
89. }
90.
91. private class baseAdapter extends
92. this);
93.
94. @Override
95. public int
96. // TODO Auto-generated method stub
97. return
98. }
99.
100. @Override
101. public Object getItem(int
102. // TODO Auto-generated method stub
103. return null;
104. }
105.
106. @Override
107. public long getItemId(int
108. // TODO Auto-generated method stub
109. return
110. }
111.
112. @Override
113. public View getView(int
114. // TODO Auto-generated method stub
115. ViewHolder holder;
116. if (convertView == null) {
117. // 使用View的对象itemView与R.layout.item关联
118. null);
119. new
120. holder.icon = (ImageView) convertView
121. .findViewById(R.id.apps_image);
122. holder.label = (TextView) convertView
123. .findViewById(R.id.apps_textview);
124. convertView.setTag(holder);
125. else
126. holder = (ViewHolder) convertView.getTag();
127. }
128.
129. holder.icon.setImageDrawable(list.get(position).getIcon());
130. holder.label.setText(list.get(position).getLabel().toString());
131.
132. return
133.
134. }
135.
136. }
137.
138. // 当用户点击应用程序图标时,将对这个类做出响应
139. private class ClickListener implements
140.
141. @Override
142. public void onItemClick(AdapterView<?> arg0, View arg1, int
143. long
144.
145. // 将应用所选的应用程序信息共享到Application中
146. MyApp appState = ((MyApp) getApplicationContext());
147. // 获取当前所在选项卡
148. String tab_id = appState.getTab_id();
149. // 设置所选应用程序信息
150. appState.set_AppInfo(tab_id, list.get(arg2).getLabel(), list.get(
151. arg2).getIcon(), list.get(arg2).getPackageName());
152. new Intent(Apps.this, Game.class);
153. startActivity(intent);
154. // 销毁当前Activity
155. finish();
156. }
157.
158. }
159.
160. // 自定义一个 AppsItemInfo 类,用来存储应用程序的相关信息
161. private class
162.
163. private Drawable icon; // 存放图片
164. private String label; // 存放应用程序名
165. private String packageName; // 存放应用程序包名
166.
167. public
168. return
169. }
170.
171. public void
172. this.icon = icon;
173. }
174.
175. public
176. return
177. }
178.
179. public void
180. this.label = label;
181. }
182.
183. public
184. return
185. }
186.
187. public void
188. this.packageName = packageName;
189. }
190.
191. }
192.
193. }