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