Android 获取Launcher 启动列表

先留着有时间再看

获取Launcher 启动列表

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

  [代码 步骤]

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

  Java代码

 

1  publicclass LauncherItem {
2
3   Drawable icon;
4
5   String name;
6
7   ComponentName component;
8
9   LauncherItem(Drawable d, String s,ComponentName cn){
10
11   icon = d;
12
13   name = s;
14
15   component = cn;
16
17   }
18
19   };
20
21   publicclass LauncherItem {
22
23   Drawable icon;
24
25   String name;
26
27   ComponentName component;
28
29   LauncherItem(Drawable d, String s,ComponentName cn){
30
31   icon = d;
32
33   name = s;
34
35   component = cn;
36
37   }
38
39   };
40  

 

2. 定义Listlvalue 用于存放查询结果

Java代码

 

publicvoid addLauncher(){

  lvalue
=new ArrayList();
  pkgMgt
=this.getPackageManager();

  
//to query all launcher & load into List<>

  Intent it
=new Intent(Intent.ACTION_MAIN);

  it.addCategory(Intent.CATEGORY_LAUNCHER);

  List ra
=pkgMgt.queryIntentActivities(it,0);
  
for(int i=0;i< p>
  ActivityInfo ai
= ra.get(i).activityInfo;

  
//String ainfo = ai.toString();

  Drawable icon
= ai.loadIcon(pkgMgt);

  String label
= ai.loadLabel(pkgMgt).toString();

  ComponentName c
=new ComponentName(ai.applicationInfo.packageName,ai.name);

  LauncherItem item
=new LauncherItem(icon,label,c);

  lvalue.add(item);

  }

  }

  
publicvoid addLauncher(){

  lvalue
=new ArrayList();
  pkgMgt
=this.getPackageManager();

  
//to query all launcher & load into List<>

 Intent it
=new Intent(Intent.ACTION_MAIN);

  it.addCategory(Intent.CATEGORY_LAUNCHER);

  List ra
=pkgMgt.queryIntentActivities(it,0);
  
for(int i=0;i< p>
  ActivityInfo ai
= ra.get(i).activityInfo;

  
//String ainfo = ai.toString();

  Drawable icon
= ai.loadIcon(pkgMgt);

  String label
= ai.loadLabel(pkgMgt).toString();

  ComponentName c
=new ComponentName(ai.applicationInfo.packageName,ai.name);

  LauncherItem item
=new LauncherItem(icon,label,c);

  lvalue.add(item);

  }

  }

 

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

  Java代码

 

 publicclass LauncherAdapter extends BaseAdapter {

  Activity activity;

  
public LauncherAdapter(Activity a){

  activity
= a;

  }

  @Override

  
publicint getCount() {

  
// TODO Auto-generated method stub

  
return lvalue.size();

  }

  @Override

  
public Object getItem(int arg0) {

  
// TODO Auto-generated method stub

  
return arg0;

  }

  @Override

  
publiclong getItemId(int position) {

  
// TODO Auto-generated method stub

  
return position;

  }

  @Override

  
public View getView(int position, View convertView, ViewGroup parent) {

  
// TODO Auto-generated method stub

  
return composeItem(position);

  }

  
public View composeItem(int position){

  LinearLayout layout
=new LinearLayout(activity);

  layout.setOrientation(LinearLayout.HORIZONTAL);

  ImageView iv
=new ImageView(activity);

  iv.setImageDrawable(lvalue.get(position).icon);

  layout.addView(iv);

  TextView tv
=new TextView(activity);

  tv.setText(lvalue.get(position).name);

 tv.setPadding(
10, 5, 0, 0);

  layout.addView(tv);

  
return layout;

  }

  }

  
publicclass LauncherAdapter extends BaseAdapter {

  Activity activity;

  
public LauncherAdapter(Activity a){

  activity
= a;

  }

  @Override

  
publicint getCount() {

  
// TODO Auto-generated method stub

  
return lvalue.size();

  }

  @Override

  
public Object getItem(int arg0) {

  
// TODO Auto-generated method stub

  
return arg0;

  }

  @Override

  
publiclong getItemId(int position) {

  
// TODO Auto-generated method stub

  
return position;

  }

  @Override

  
public View getView(int position, View convertView, ViewGroup parent) {

  
// TODO Auto-generated method stub

  
return composeItem(position);

  }

  
public View composeItem(int position){

  LinearLayout layout
=new LinearLayout(activity);

  layout.setOrientation(LinearLayout.HORIZONTAL);

  ImageView iv
=new ImageView(activity);

  iv.setImageDrawable(lvalue.get(position).icon);

  layout.addView(iv);

  TextView tv
=new TextView(activity);

  tv.setText(lvalue.get(position).name);

  tv.setPadding(
10, 5, 0, 0);

  layout.addView(tv);

  
return layout;

  }

  }

 

4. 启动某个item 当单击时

  Java代码

adapter =new LauncherAdapter(this);

  lv.setAdapter(adapter);

  lv.setOnItemClickListener(
new OnItemClickListener(){

  @Override

  
publicvoid onItemClick(AdapterView arg0, View arg1, int arg2,

  
long arg3) {

  
// TODO Auto-generated method stub

  Intent intent
=new Intent(Intent.ACTION_VIEW);

  intent.setComponent(lvalue.get(arg2).component);
 startActivity(intent);

  }

  });

  adapter
=new LauncherAdapter(this);

  lv.setAdapter(adapter);

  lv.setOnItemClickListener(
new OnItemClickListener(){

  @Override

  
publicvoid onItemClick(AdapterView arg0, View arg1, int arg2,

  
long arg3) {

  
// TODO Auto-generated method stub

  Intent intent
=new Intent(Intent.ACTION_VIEW);

  intent.setComponent(lvalue.get(arg2).component);

  startActivity(intent);

  }

  });

 

5. emulator 结果结果

  - 列出所有application

 

- 单击Alarm Clock 的情形:

 

posted @ 2010-09-14 18:41  飞翔的熊猫  阅读(2252)  评论(0编辑  收藏  举报