写一个 android launcher界面

看了网上 PackageManager 和 ActivityManager ,看到有一个列出所有App的例子,就做了下面的launcher


第一步骤:

首先新建一个工程,默认就一个Activity 就是MainActivity

在AndroidManifest.xml的MainActivity节点下新添加



第二步骤:

主界面只有一个 GridView



    
    

 
  
第三步骤:

定义每一个app的界面,app只显示icon和name




    
    

    
    


第四步骤:

定义GridView的适配器


public class AppAdapter extends BaseAdapter {
	private Context mContext;
	private LayoutInflater inflater;
	ArrayList appList;

	public final class AppItem {	
		public ImageView appIcon;
		public TextView appName;		
	}

	public AppAdapter(Context context, List apps) {
		mContext = context;
		inflater = LayoutInflater.from(mContext);		
		appList = (ArrayList)apps;
	}

	@Override
	public int getCount() {
		return appList.size();
	}

	@Override
	public Object getItem(int position) {
		return appList.get(position); 
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		AppItem appItem = null;

		if(convertView == null)
		{  
			convertView = inflater.inflate(R.layout.app_item, null);
			appItem = new AppItem();
			appItem.appIcon = (ImageView) convertView.findViewById(R.id.app_icon);		
			appItem.appName = (TextView) convertView.findViewById(R.id.app_name);	
			convertView.setTag(appItem);
		} else {  		
			appItem = (AppItem) convertView.getTag();
		}  

		ResolveInfo info = appList.get(position); 		
		appItem.appIcon.setImageDrawable(info.loadIcon(mContext.getPackageManager()));
		appItem.appName.setText(info.loadLabel(mContext.getPackageManager()));

		return convertView;
	}
}

通过 ResolveInfo 获取具体信息方法:

包名获取方法:resolve.activityInfo.packageName

icon获取获取方法:resolve.loadIcon(packageManager)

应用名称获取方法:resolve.loadLabel(packageManager).toString()


第五步骤:

主界面代码:

public class MainActivity extends Activity {

	private GridView appsGridView;  
	private List appList;  

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);        
		appsGridView = (GridView) findViewById(R.id.apps_list);         

		//获取app列表
		appList = getAppList();
		appsGridView.setAdapter(new AppAdapter(MainActivity.this, appList));
		appsGridView.setOnItemClickListener(new OnItemClickListener(){
			@Override
			public void onItemClick(AdapterView parent, View view,
					int position, long id) {			
				ResolveInfo info = appList.get(position); 
				openAppLication(info);
			}
		});
	}

	//获取APP列表
	private List getAppList(){    
		Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);  
		mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);  
		return getPackageManager().queryIntentActivities(mainIntent, 0);	
	}

	//打开App
	private void openAppLication(ResolveInfo info){
		
		//该应用的包名  
		String pkg = info.activityInfo.packageName; 

		//应用的主activity类(MAIN)  
		String cls = info.activityInfo.name;  

		//ComponentName(组件名称)是用来打开其他应用程序中的Activity或服务的。
		ComponentName componet = new ComponentName(pkg, cls);  
		Intent intent = new Intent();  
		intent.setComponent(componet);  

		startActivity(intent);
	}
}

获取app列表

Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);  
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);  
getPackageManager().queryIntentActivities(mainIntent, 0);


打开对应的应用

//该应用的包名  
String pkg = info.activityInfo.packageName; 

//应用的主activity类(MAIN)  
String cls = info.activityInfo.name;  

//ComponentName(组件名称)是用来打开其他应用程序中的Activity或服务的。
ComponentName componet = new ComponentName(pkg, cls);  
Intent intent = new Intent();  
intent.setComponent(componet);  

startActivity(intent);









你可能感兴趣的:(Android)