论坛首页 移动开发技术论坛

获取Launcher 启动列表

浏览 9838 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2010-06-22  

获取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 的情形:

 

 

 

 

有问题请跟帖  否则 请顶贴  谢谢!

 

 

 

   发表时间:2010-06-22  
先感谢楼主,但我没有调通过这些代码,请楼主再把代码打包放出来好吗?
0 请登录后投票
   发表时间:2010-08-21  
跟楼主请教个菜鸟问题。
我想查看下PackageManager 的queryIntentActivities方法是怎样实现的,但发现PackageManager 是抽象类,queryIntentActivities是抽象方法,获得PackageManager实例的getPackageManager方法,是ContextWrapper的方法,但在ContextWrapper中没有发现有setPackageManager或createPackageManager之类的方法,那我怎样才能找到PackageManager 的实现类呢
0 请登录后投票
   发表时间:2010-09-01  
有点像进程管家,是吧?
0 请登录后投票
   发表时间:2010-11-17  
楼主可否把源码发给我看看  hbsz_zl_love@126.com   谢谢了
0 请登录后投票
   发表时间:2010-11-21  
有什么办法可能拿到刚刚下载安装的应用程序(apk)的基本信息吗?比如AppName,icon,并放入指定的listView里面
0 请登录后投票
   发表时间:2011-04-18  
呵呵 真不错哈
0 请登录后投票
   发表时间:2011-04-18  
有个很好的方法可以试试,2.0以后才有的 ^_^
PackageManager pm;
Intent intent = pm.getLaunchIntentForPackage(packageName);
0 请登录后投票
   发表时间:2011-04-26  
学习了,感谢分享
0 请登录后投票
   发表时间:2011-06-15  
不错,学习了。。。
0 请登录后投票
论坛首页 移动开发技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics