launcher2

android手把手教你开发launcher (二)

第二课:列出安装的应用程序


预备知识: 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" ?>
< LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
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;
}
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值