android手把手教你开发launcher (二)
第二课:列出安装的应用程序
预备知识: GridView的使用 \ 改写BaseAdapter
列出已经安装的应用程序是作为launcher比不可少的功能。下面我们就讲解怎样将应用程序列出来。程序运行后的样子如下:
一. 修改main.xml,在其中添加一个GridView用来显示应用程序列表。
修改后如下:
二 . 通过PackageManager的api 查询已经安装的apk
我们写一个叫做loadApps的方法将活得的应用程序列表放到private List<ResolveInfo> mApps; 中,如下:
‘
三. 实现用于显示Gridview的Adapter,使其显示获得的应用程序列表
直接上代码:
最后整个Activity的代码如下
第二课:列出安装的应用程序
预备知识: GridView的使用 \ 改写BaseAdapter
列出已经安装的应用程序是作为launcher比不可少的功能。下面我们就讲解怎样将应用程序列出来。程序运行后的样子如下:

一. 修改main.xml,在其中添加一个GridView用来显示应用程序列表。
修改后如下:
1
2
3
4
5
6
7
8
9
10
11
12
|
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
android:orientation
=
"vertical"
android:layout_width
=
"fill_parent"
android:layout_height
=
"fill_parent"
>
<
GridView
android:layout_width
=
"match_parent"
android:id
=
"@+id/apps_list"
android:numColumns
=
"4"
android:layout_height
=
"wrap_content"
>
</
GridView
>
</
LinearLayout
>
|
二 . 通过PackageManager的api 查询已经安装的apk
我们写一个叫做loadApps的方法将活得的应用程序列表放到private List<ResolveInfo> mApps; 中,如下:
1
2
3
4
5
6
|
private
void
loadApps() {
Intent mainIntent =
new
Intent(Intent.ACTION_MAIN,
null
);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
mApps = getPackageManager().queryIntentActivities(mainIntent,
0
);
}
|
三. 实现用于显示Gridview的Adapter,使其显示获得的应用程序列表
直接上代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
public
class
AppsAdapter
extends
BaseAdapter {
public
AppsAdapter() {
}
public
View getView(
int
position, View convertView, ViewGroup parent) {
ImageView i;
if
(convertView ==
null
) {
i =
new
ImageView(MyHome.
this
);
i.setScaleType(ImageView.ScaleType.FIT_CENTER);
i.setLayoutParams(
new
GridView.LayoutParams(
50
,
50
));
}
else
{
i = (ImageView) convertView;
}
ResolveInfo info = mApps.get(position);
i.setImageDrawable(info.activityInfo.loadIcon(getPackageManager()));
return
i;
}
public
final
int
getCount() {
return
mApps.size();
}
public
final
Object getItem(
int
position) {
return
mApps.get(position);
}
public
final
long
getItemId(
int
position) {
return
position;
}
}
|
最后整个Activity的代码如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
package
org.bangchui.myhome;
import
java.util.List;
import
android.app.Activity;
import
android.content.Intent;
import
android.content.pm.ResolveInfo;
import
android.os.Bundle;
import
android.view.View;
import
android.view.ViewGroup;
import
android.widget.BaseAdapter;
import
android.widget.GridView;
import
android.widget.ImageView;
public
class
MyHome
extends
Activity {
GridView mGrid;
/** Called when the activity is first created. */
@Override
public
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
loadApps();
setContentView(R.layout.main);
mGrid = (GridView) findViewById(R.id.apps_list);
mGrid.setAdapter(
new
AppsAdapter());
}
private
List<ResolveInfo> mApps;
private
void
loadApps() {
Intent mainIntent =
new
Intent(Intent.ACTION_MAIN,
null
);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
mApps = getPackageManager().queryIntentActivities(mainIntent,
0
);
}
public
class
AppsAdapter
extends
BaseAdapter {
public
AppsAdapter() {
}
public
View getView(
int
position, View convertView, ViewGroup parent) {
ImageView i;
if
(convertView ==
null
) {
i =
new
ImageView(MyHome.
this
);
i.setScaleType(ImageView.ScaleType.FIT_CENTER);
i.setLayoutParams(
new
GridView.LayoutParams(
50
,
50
));
}
else
{
i = (ImageView) convertView;
}
ResolveInfo info = mApps.get(position);
i.setImageDrawable(info.activityInfo.loadIcon(getPackageManager()));
return
i;
}
public
final
int
getCount() {
return
mApps.size();
}
public
final
Object getItem(
int
position) {
return
mApps.get(position);
}
public
final
long
getItemId(
int
position) {
return
position;
}
}
}
|