android 获取lanucher 列表

引用:http://www.iteye.com/topic/696187

获取Launcher 启动列表

即 列出所有Launcher程序 通过PackageManager 来获取

[代码 步骤]

1. 定义内部类 LauncherItem  用于定义Application相关属性 比如:图标 名称 以及 ComponentName

Java代码  收藏代码
  1. public class LauncherItem {  
  2.         Drawable icon;  
  3.         String name;  
  4.         ComponentName component;  
  5.           
  6.         LauncherItem(Drawable d, String s,ComponentName cn){  
  7.             icon = d;  
  8.             name = s;  
  9.             component = cn;  
  10.         }  
  11.     };  

2.  定义List<LauncherItem> lvalue 用于存放查询结果

Java代码  收藏代码
  1. public void addLauncher(){  
  2.         lvalue = new ArrayList<LauncherItem>();  
  3.           
  4.         pkgMgt = this.getPackageManager();  
  5.           
  6.         //to query all launcher & load into List<>  
  7.         Intent it = new Intent(Intent.ACTION_MAIN);  
  8.         it.addCategory(Intent.CATEGORY_LAUNCHER);   
  9.           
  10.         List<ResolveInfo> ra =pkgMgt.queryIntentActivities(it,0);  
  11.           
  12.         for(int i=0;i<ra.size();i++){  
  13.             ActivityInfo ai = ra.get(i).activityInfo;  
  14.               
  15.             //String ainfo = ai.toString();  
  16.             Drawable icon = ai.loadIcon(pkgMgt);  
  17.             String label = ai.loadLabel(pkgMgt).toString();  
  18.             ComponentName c = new ComponentName(ai.applicationInfo.packageName,ai.name);  
  19.               
  20.               
  21.             LauncherItem item = new LauncherItem(icon,label,c);  
  22.               
  23.             lvalue.add(item);  
  24.         }  
  25.           
  26.     }  

3. 定义LauncherAdapter 并指定各个item显示样式

Java代码  收藏代码
  1. public class LauncherAdapter extends BaseAdapter {  
  2.         Activity activity;  
  3.           
  4.         public LauncherAdapter(Activity a){  
  5.             activity = a;  
  6.         }  
  7.           
  8.         @Override  
  9.         public int getCount() {  
  10.             // TODO Auto-generated method stub  
  11.             return lvalue.size();  
  12.         }  
  13.   
  14.         @Override  
  15.         public Object getItem(int arg0) {  
  16.             // TODO Auto-generated method stub  
  17.             return arg0;  
  18.         }  
  19.   
  20.         @Override  
  21.         public long getItemId(int position) {  
  22.             // TODO Auto-generated method stub  
  23.             return position;  
  24.         }  
  25.   
  26.         @Override  
  27.         public View getView(int position, View convertView, ViewGroup parent) {  
  28.             // TODO Auto-generated method stub  
  29.             return composeItem(position);  
  30.         }  
  31.           
  32.         public View composeItem(int position){  
  33.             LinearLayout layout = new LinearLayout(activity);  
  34.             layout.setOrientation(LinearLayout.HORIZONTAL);  
  35.               
  36.             ImageView iv = new ImageView(activity);  
  37.             iv.setImageDrawable(lvalue.get(position).icon);  
  38.             layout.addView(iv);  
  39.               
  40.             TextView tv = new TextView(activity);  
  41.             tv.setText(lvalue.get(position).name);  
  42.             tv.setPadding(10500);  
  43.             layout.addView(tv);  
  44.               
  45.             return layout;  
  46.         }  
  47.           
  48.           
  49.           
  50.     }  

4. 启动某个item 当单击时

Java代码  收藏代码
  1. adapter = new LauncherAdapter(this);  
  2.         lv.setAdapter(adapter);  
  3.           
  4.         lv.setOnItemClickListener(new OnItemClickListener(){  
  5.   
  6.             @Override  
  7.             public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,  
  8.                     long arg3) {  
  9.                 // TODO Auto-generated method stub  
  10.                 Intent intent =new Intent(Intent.ACTION_VIEW);    
  11.                 intent.setComponent(lvalue.get(arg2).component);  
  12.                 startActivity(intent);  
  13.             }  
  14.               
  15.         });  

  

5.  emulator 结果结果

- 列出所有application

- 单击Alarm Clock 的情形:

原文地址:https://www.cnblogs.com/sode/p/3001697.html