有的时候我们需要获取系统的应用程序,比如说Home界面的时候,我们需要获取在Manifest里面注册的所有的应用的信息,并将这些应用的图标,名字显示出来,同时还要获取他们的action或者CompanentName,以便点击他们的时候可以跳转到相应的应用。
下面就是一个例子:
先上运行出来的效果图:
很明显,这需要一个ListView,看一下layout文件:
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="match_parent"
5 android:layout_height="match_parent">
6
7 <TextView
8 android:layout_width="match_parent"
9 android:layout_height="wrap_content"
10 android:text="@string/hello"
11 android:textColor="@android:color/white"
12 android:textSize="24sp"/>
13
14 <ListView
15 android:id="@+id/mylist"
16 android:layout_width="match_parent"
17 android:layout_height="0dip"
18 android:layout_weight="1"/>
19 </LinearLayout>
我们需要一个文件来显示应用程序的信息:比如应用的名字,图标,已经点击的Intent等,如下所示:
1 package com.android.test;
2
3 import android.content.ComponentName;
4 import android.graphics.drawable.Drawable;
5
6 public class ApplicationInfo {
7
8 String name;
9 ComponentName intent;
10 Drawable icon;
11
12 public String getName () {
13 return name;
14 }
15
16 public void setName (String name) {
17 this.name = name;
18 }
19
20 public ComponentName getIntent () {
21 return intent;
22 }
23
24 public void setIntent (ComponentName intent) {
25 this.intent = intent;
26 }
27
28 public Drawable getIcon () {
29 return icon;
30 }
31
32 public void setIcon (Drawable icon) {
33 this.icon = icon;
34 }
35 }
好了,现在我们要开始获取应用的信息了,并把获得的信息封装在一个List<ApplicationInfo>中,具体是这样的:
1 private List<ApplicationInfo> loadAppInfomation(Context context) {
2 List<ApplicationInfo> apps = new ArrayList<ApplicationInfo>();
3 PackageManager pm = context.getPackageManager();
4 Intent intent = new Intent(Intent.ACTION_MAIN, null);
5 intent.addCategory(Intent.CATEGORY_LAUNCHER);
6 List<ResolveInfo> infos = pm.queryIntentActivities(intent, 0);
7 Collections.sort(infos, new ResolveInfo.DisplayNameComparator(pm));
8 if(infos != null) {
9 apps.clear();
10 for(int i=0; i<infos.size(); i++) {
11 ApplicationInfo app = new ApplicationInfo();
12 ResolveInfo info = infos.get(i);
13 app.setName(info.loadLabel(pm).toString());
14 app.setIcon(info.loadIcon(pm));
15 app.setIntent(new ComponentName(info.activityInfo.packageName, info.activityInfo.name));
16 apps.add(app);
17 }
18 }
19 return apps;
20 }
首先是拿到packageManager,然后扫描带有Action_main和Category_Launcher的Intent_filter.查询到的返回值是一个List<ResolveInfo>,里面的每一个ResolveInfo就是一个应用的信息。
然后我们需要遍历这个List,拿到每一个应用的相关信息(我们需要的那些信息),然后将拿到的我们需要的信息放在一个ApplicationInfo对象中,然后讲所有的应用的ApplicationInfo放到一个List里面,这个List里面就是我们需要的数据。
然后我们需要写一个Adapter,将数据传递进去,让ListView现实出我们需要显示的东西:
Adapter如下:
1 package com.android.test;
2
3 import java.util.List;
4
5 import android.content.Context;
6 import android.view.LayoutInflater;
7 import android.view.View;
8 import android.view.ViewGroup;
9 import android.widget.BaseAdapter;
10 import android.widget.ImageView;
11 import android.widget.TextView;
12
13 public class ApplicationAdapter extends BaseAdapter {
14
15 private List<ApplicationInfo> apps;
16 private LayoutInflater inflater;
17
18 public ApplicationAdapter (Context context, List<ApplicationInfo> infos) {
19 this.apps = infos;
20 inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
21 }
22
23 @Override
24 public int getCount () {
25 return apps.size();
26 }
27
28 @Override
29 public Object getItem (int position) {
30 return position;
31 }
32
33 @Override
34 public long getItemId (int position) {
35 return position;
36 }
37
38 @Override
39 public View getView (int position, View convertView, ViewGroup parent) {
40 ViewHolder holder;
41 if(convertView == null) {
42 holder = new ViewHolder();
43 convertView = inflater.inflate(R.layout.app_adapter_list_item, null);
44 holder.icon = (ImageView) convertView.findViewById(R.id.app_icon);
45 holder.name = (TextView) convertView.findViewById(R.id.app_name);
46 convertView.setTag(holder);
47 } else {
48 holder = (ViewHolder) convertView.getTag();
49 }
50 holder.icon.setImageDrawable(apps.get(position).getIcon());
51 holder.name.setText(apps.get(position).getName());
52 return convertView;
53 }
54
55 class ViewHolder {
56 ImageView icon;
57 TextView name;
58 }
59 }
Adapter里面就是getView里面显示每一个Item项,这里我就不在多废话了,这里我们需要显示应用的图标和名字,所以在布局中我们需要一个ImageView和一个TextView,Adapter的item项的布局文件如下:
1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout
3 xmlns:android="http://schemas.android.com/apk/res/android"
4 android:layout_width="match_parent"
5 android:layout_height="match_parent"
6 android:orientation="horizontal">
7 <ImageView
8 android:id="@+id/app_icon"
9 android:layout_width="wrap_content"
10 android:layout_height="wrap_content"/>
11 <TextView
12 android:id="@+id/app_name"
13 android:layout_width="0dip"
14 android:layout_weight="1"
15 android:gravity="center"
16 android:textColor="@android:color/white"
17 android:textSize="20sp"
18 android:layout_height="wrap_content"/>
19 </LinearLayout>
这里整个ListView就已经可以显示出来了,我们还需要写点击事件:
1 @Override
2 public void onItemClick (AdapterView<?> parent, View view, int position, long id) {
3 Intent intent = new Intent();
4 intent.setComponent(apps.get(position).getIntent());
5 startActivity(intent);
6 }
下面把主Activity放出来:
1 package com.android.test;
2
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.List;
6
7 import android.app.Activity;
8 import android.content.ComponentName;
9 import android.content.Context;
10 import android.content.Intent;
11 import android.content.pm.PackageManager;
12 import android.content.pm.ResolveInfo;
13 import android.os.Bundle;
14 import android.view.View;
15 import android.widget.AdapterView;
16 import android.widget.AdapterView.OnItemClickListener;
17 import android.widget.ListView;
18
19 public class GetApplicationOfSystem extends Activity implements OnItemClickListener {
20
21 private ListView mListView;
22 private ApplicationAdapter mAdapter;
23 private List<ApplicationInfo> apps;
24
25 @Override
26 protected void onCreate (Bundle savedInstanceState) {
27 super.onCreate(savedInstanceState);
28 setContentView(R.layout.system_app);
29
30 mListView = (ListView) findViewById(R.id.mylist);
31 mListView.setOnItemClickListener(this);
32 apps = loadAppInfomation(this);
33 mAdapter = new ApplicationAdapter(this, apps);
34 mListView.setAdapter(mAdapter);
35 }
36
37 private List<ApplicationInfo> loadAppInfomation(Context context) {
38 List<ApplicationInfo> apps = new ArrayList<ApplicationInfo>();
39 PackageManager pm = context.getPackageManager();
40 Intent intent = new Intent(Intent.ACTION_MAIN, null);
41 intent.addCategory(Intent.CATEGORY_LAUNCHER);
42 List<ResolveInfo> infos = pm.queryIntentActivities(intent, 0);
43 Collections.sort(infos, new ResolveInfo.DisplayNameComparator(pm));
44 if(infos != null) {
45 apps.clear();
46 for(int i=0; i<infos.size(); i++) {
47 ApplicationInfo app = new ApplicationInfo();
48 ResolveInfo info = infos.get(i);
49 app.setName(info.loadLabel(pm).toString());
50 app.setIcon(info.loadIcon(pm));
51 app.setIntent(new ComponentName(info.activityInfo.packageName, info.activityInfo.name));
52 apps.add(app);
53 }
54 }
55 return apps;
56 }
57
58 @Override
59 public void onItemClick (AdapterView<?> parent, View view, int position, long id) {
60 Intent intent = new Intent();
61 intent.setComponent(apps.get(position).getIntent());
62 startActivity(intent);
63 }
64 }
还有一种情况是这样的,我们有时需要获取安装的应用的信息,而不是整个系统的应用的信息,
这是需要换一种方式来获取安装的应用的信息:
1 private List<PackageInfo> loadPackageInfo(Context context) {
2 List<PackageInfo> apps = new ArrayList<PackageInfo>();
3 PackageManager pm = context.getPackageManager();
4 List<PackageInfo> packageList = pm.getInstalledPackages(0);
5 for(int i=0; i<packageList.size(); i++) {
6 PackageInfo info = packageList.get(i);
7 if((info.applicationInfo.flags & info.applicationInfo.FLAG_SYSTEM) <= 0) {
8 apps.add(info);
9 }
10 }
11 return apps;
12 }
从本地的包中可以获取安装的包,注意,这里的安装的包是指说有的包,包括系统的包,我们只需要我们自己安装的,不需要系统的,怎么办呢,这时我们需要将系统的包过滤掉,我们可以做判断如果if((info.applicationInfo.flags & info.application.FLAG_SYSTEM) <= 0),就说明不是系统的包,是后来安装上去的,我们就可以把它加到List中去了。
获取安装的包的Adapter:
1 package com.android.test;
2
3 import java.util.List;
4
5 import android.content.Context;
6 import android.content.pm.PackageInfo;
7 import android.view.LayoutInflater;
8 import android.view.View;
9 import android.view.ViewGroup;
10 import android.widget.BaseAdapter;
11 import android.widget.ImageView;
12 import android.widget.TextView;
13
14 public class InstalledPackageAdapter extends BaseAdapter {
15
16 private List<PackageInfo> mApps;
17 private LayoutInflater inflater;
18 private Context mContext;
19
20 public InstalledPackageAdapter (Context context, List<PackageInfo> infos) {
21 this.mContext = context;
22 this.mApps = infos;
23 inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
24 }
25
26 @Override
27 public int getCount () {
28 return mApps.size();
29 }
30
31 @Override
32 public Object getItem (int position) {
33 return position;
34 }
35
36 @Override
37 public long getItemId (int position) {
38 return position;
39 }
40
41 @Override
42 public View getView (int position, View convertView, ViewGroup parent) {
43 ViewHolder holder;
44 if(convertView == null) {
45 holder = new ViewHolder();
46 convertView = inflater.inflate(R.layout.app_adapter_list_item, null);
47 holder.icon = (ImageView) convertView.findViewById(R.id.app_icon);
48 holder.name = (TextView) convertView.findViewById(R.id.app_name);
49 convertView.setTag(holder);
50 } else {
51 holder = (ViewHolder) convertView.getTag();
52 }
53 holder.icon.setImageDrawable(mApps.get(position).applicationInfo.loadIcon(mContext.getPackageManager()));
54 holder.name.setText(mApps.get(position).applicationInfo.loadLabel(mContext.getPackageManager()));
55 return convertView;
56 }
57
58 class ViewHolder {
59 ImageView icon;
60 TextView name;
61 }
62 }
获取安装的应用的Activity:
1 package com.android.test;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import android.app.Activity;
7 import android.content.Context;
8 import android.content.Intent;
9 import android.content.pm.PackageInfo;
10 import android.content.pm.PackageManager;
11 import android.os.Bundle;
12 import android.view.View;
13 import android.widget.AdapterView;
14 import android.widget.AdapterView.OnItemClickListener;
15 import android.widget.ListView;
16
17 public class GetApplicationOfInstalled extends Activity implements OnItemClickListener {
18
19 private ListView mListView;
20 private InstalledPackageAdapter maAdapter;
21 private List<PackageInfo> mApps;
22
23 @Override
24 protected void onCreate (Bundle savedInstanceState) {
25 super.onCreate(savedInstanceState);
26 setContentView(R.layout.installed_app);
27 mListView = (ListView) findViewById(R.id.mylist);
28 mListView.setOnItemClickListener(this);
29 mApps = loadPackageInfo(this);
30 maAdapter = new InstalledPackageAdapter(this, mApps);
31 mListView.setAdapter(maAdapter);
32 }
33
34 private List<PackageInfo> loadPackageInfo(Context context) {
35 List<PackageInfo> apps = new ArrayList<PackageInfo>();
36 PackageManager pm = context.getPackageManager();
37 List<PackageInfo> packageList = pm.getInstalledPackages(0);
38 for(int i=0; i<packageList.size(); i++) {
39 PackageInfo info = packageList.get(i);
40 if((info.applicationInfo.flags & info.applicationInfo.FLAG_SYSTEM) <= 0) {
41 apps.add(info);
42 }
43 }
44 return apps;
45 }
46
47 @Override
48 public void onItemClick (AdapterView<?> parent, View view, int position, long id) {
49 Intent intent = new Intent();
50 PackageInfo packageInfo = mApps.get(position);
51 startActivity(intent);
52 }
53 }
获取安装的应用的效果图:
好了,就写这么多吧。。。
源代码下载地址如下: