展会信息港展会大全

Android获取Launcher启动列表
来源:互联网   发布日期:2016-01-14 12:31:42   浏览:2337次  

导读:获取Launcher启动列表,即列出所有Launcher程序,通过PackageManager来获� ?代码步骤]1、定义内部类LauncherItem用于定义Application相关属性,比如:图标、名称、以及ComponentNamepublic class LauncherIt......

获取Launcher启动列表,即列出所有Launcher程序,通过PackageManager来获龋

[代码步骤]

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

public class LauncherItem {

Drawable icon;

String name;

ComponentName component;

LauncherItem(Drawable d, String s,ComponentName cn){

icon = d;

name = s;

component = cn;

}

};

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

public void addLauncher(){

lvalue = new ArrayList<LAUNCHERITEM>();

pkgMgt = this.getPackageManager();

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

Intent it = new Intent(Intent.ACTION_MAIN);

it.addCategory(Intent.CATEGORY_LAUNCHER);

List<RESOLVEINFO> ra=pkgMgt.queryIntentActivities(it,0);

for(int i=0; i < ra.size(); i++){

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显示样式

public class LauncherAdapter extends BaseAdapter {

Activity activity;

public LauncherAdapter(Activity a){

activity = a;

}

@Override

public int getCount() {

// TODO Auto-generated method stub

return lvalue.size();

}

@Override

public Object getItem(int arg0) {

// TODO Auto-generated method stub

return arg0;

}

@Override

public long 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,当单击时

adapter = new LauncherAdapter(this);

lv.setAdapter(adapter);

lv.setOnItemClickListener(new OnItemClickListener(){

@Override

public void 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的情形

赞助本站

人工智能实验室

相关热词: 获取 Launcher 启动列表

AiLab云推荐
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港