首页 > 代码库 > 安卓Launcher之获取手机安装的应用列表
安卓Launcher之获取手机安装的应用列表
Launcher中最主要的就是获取所有应用列表的入口以及图标,一般获取的方法有两种:
PackageInfo
ResolveInfo
运行获取所有APP的Launcher并且允许进行点击事件,进入到应用
下面通过这两种方法获取到所有应用的列表:
建立基本数据:
PakageMod.java
public class PakageMod { public String pakageName; public String appName; public Drawable icon; public PakageMod() { super(); } public PakageMod(String pakageName, String appName, Drawable icon) { super(); this.pakageName = pakageName; this.appName = appName; this.icon = icon; } }
public class DemoGridAdapter extends BaseAdapter { private LayoutInflater inflater; private List<PakageMod> datas; public DemoGridAdapter(Context context, List<PakageMod> datas) { super(); inflater = LayoutInflater.from(context); this.datas = datas; } @Override public int getCount() { return datas.size(); } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder; if (convertView == null) { // 使用View的对象itemView与R.layout.item关联 convertView = inflater.inflate(R.layout.apps, null); holder = new ViewHolder(); holder.icon = (ImageView) convertView.findViewById(R.id.apps_image); holder.label = (TextView) convertView .findViewById(R.id.apps_textview); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } holder.icon.setImageDrawable(datas.get(position).icon); holder.label.setText(datas.get(position).appName); return convertView; } class ViewHolder { private ImageView icon; private TextView label; } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center" android:orientation="vertical" > <ImageView android:id="@+id/apps_image" android:layout_width="48dip" android:layout_height="48dip" android:icon="@drawable/ic_launcher" /> <TextView android:id="@+id/apps_textview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:maxEms="5" android:maxLines="1" android:text="good" /> </LinearLayout>
下面在Activity中获取到列表并显示到GridView中,并点击进入应用;
使用PackageInfo
public class PackageInfoDemo extends Activity { private GridView gridview; private PackageManager pManager; private List<PakageMod> datas; private String tag = "MainActivity"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.setContentView(R.layout.activity_main); // 取得gridview gridview = (GridView) findViewById(R.id.gridview); // 获取图片、应用名、包名 pManager = PackageInfoDemo.this.getPackageManager(); List<PackageInfo> appList = getAllApps(PackageInfoDemo.this); datas = new ArrayList<PakageMod>(); for (int i = 0; i < appList.size(); i++) { PackageInfo pinfo = appList.get(i); PakageMod shareItem = new PakageMod(); // 设置图片 shareItem.icon = pManager.getApplicationIcon(pinfo.applicationInfo); // 设置应用程序名字 shareItem.appName = pManager.getApplicationLabel( pinfo.applicationInfo).toString(); // 设置应用程序的包名 shareItem.pakageName = pinfo.applicationInfo.packageName; datas.add(shareItem); } gridview.setAdapter(new baseAdapter(this, datas)); // 点击应用图标时,做出响应 gridview.setOnItemClickListener(new ClickListener()); } public static List<PackageInfo> getAllApps(Context context) { List<PackageInfo> apps = new ArrayList<PackageInfo>(); PackageManager pManager = context.getPackageManager(); // 获取手机内所有应用 List<PackageInfo> packlist = pManager.getInstalledPackages(0); for (int i = 0; i < packlist.size(); i++) { PackageInfo pak = (PackageInfo) packlist.get(i); // if()里的值如果<=0则为自己装的程序,否则为系统工程自带 if ((pak.applicationInfo.flags & pak.applicationInfo.FLAG_SYSTEM) <= 0) { // 添加自己已经安装的应用程序 // apps.add(pak); } apps.add(pak); } return apps; } private class ClickListener implements OnItemClickListener { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) { Intent intent = new Intent(); intent = PackageInfoDemo.this.getPackageManager() .getLaunchIntentForPackage(datas.get(position).pakageName); startActivity(intent); } } }
使用ResolveInfo
public class ResolveInfoDemo extends Activity { private GridView gridview; private PackageManager pManager; private List<PakageMod> datas; private String tag = "ResolveInfoDemo"; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); this.setContentView(R.layout.activity_main); // 取得gridview gridview = (GridView) findViewById(R.id.gridview); // 获取图片、应用名、包名 pManager = this.getPackageManager(); datas = new GetAllApps(this).getDatas(); gridview.setAdapter(new DemoGridAdapter(this, datas)); gridview.setOnItemClickListener(new ClickListener()); } // 当用户点击应用程序图标时,将对这个类做出响应 private class ClickListener implements OnItemClickListener { @Override public void onItemClick(AdapterView<?> adapterView, View view, int arg2, long arg3) { Intent intent = new Intent(); intent = getPackageManager().getLaunchIntentForPackage( datas.get(arg2).pakageName); startActivity(intent); } } }
GetAllApps.java
public class GetAllApps { private Context mContext; private PackageManager packageManager; private int mIconDpi; private String tag = "GetAllApps"; private List<PakageMod> datas = new ArrayList<PakageMod>(); public GetAllApps(Context mContext){ this.mContext = mContext; ActivityManager activityManager = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE); packageManager = mContext.getPackageManager(); mIconDpi = activityManager.getLauncherLargeIconDensity(); } public void loadAllAppsByBatch() { List<ResolveInfo> apps = null; Intent mainIntent = new Intent(Intent.ACTION_MAIN, null); mainIntent.addCategory(Intent.CATEGORY_LAUNCHER); apps = packageManager.queryIntentActivities(mainIntent, 0); for (int i = 0; i < apps.size(); i++) { String packageName = apps.get(i).activityInfo.applicationInfo.packageName; String title = apps.get(i).loadLabel(packageManager).toString(); Drawable icon = null; if(title == null){ title = apps.get(i).activityInfo.name; } ActivityInfo info = apps.get(i).activityInfo; icon = getFullResIcon(info); datas.add(new PakageMod(packageName,title,icon)); } } public Drawable getFullResIcon(ActivityInfo info) { Resources resources; try { resources = packageManager.getResourcesForApplication( info.applicationInfo); } catch (PackageManager.NameNotFoundException e) { resources = null; } if (resources != null) { int iconId = info.getIconResource(); if (iconId != 0) { return getFullResIcon(resources, iconId); } } return getFullResDefaultActivityIcon(); } public Drawable getFullResDefaultActivityIcon() { return getFullResIcon(Resources.getSystem(), android.R.mipmap.sym_def_app_icon); } public Drawable getFullResIcon(Resources resources, int iconId) { Drawable d; try { // requires API level 15 (current min is 14): d = resources.getDrawableForDensity(iconId, mIconDpi); } catch (Resources.NotFoundException e) { d = null; } return (d != null) ? d : getFullResDefaultActivityIcon(); } public List<PakageMod> getDatas() { loadAllAppsByBatch(); return datas; } }
这里getDrawableForDensity需要是15版本以上的SDK支持,所有低版本的Launcher不可以使用;
本博文使用的两种方法都不需要配置任何权限;
Android4.2的Launcher中使用ResolveInfo进行获取所用的应用列表,这里的ResolveInfo的Demo也是从Launcher源码中抄出来的;
或许还有第3种方法.......待续;
本文来自于CSDN博客,转载请联系作者;
注明出处http://blog.csdn.net/dreamintheworld/article/details/39718581
安卓Launcher之获取手机安装的应用列表
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。