大家好,我今天给大家简单讲一下Launcher里如何列出所有安装的应用的,我们点击Launcher的抽屉(SlidingDrawer)就会 出现所有安装的应用列表(当然Widget,Launcher本身除外).并且点击应用图标进入 相关应用。我这里就先简单的用一个GridView来显示应用。
老样子我还会写一个简单的Demo让大家理解。大家跟着我的步骤来。
第一步:新建一个Android工程命名为:Launcher.
第二步:修改main.xml布局文件,代码如下(只有一个GridView这里):
- <?xml version= "1.0" encoding= "utf-8" ?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <GridView
- android:id="@+id/allapps"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- />
- </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <GridView android:id="@+id/allapps" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>
第三步:新建一个application_layout.xml布局文件,用来定义应用的图标和标题,代码如下:
- <?xml version= "1.0" encoding= "utf-8" ?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <ImageView
- android:id="@+id/app_icon"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- />
- <TextView
- android:id="@+id/app_title"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- />
- </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ImageView android:id="@+id/app_icon" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/app_title" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
第四步:也就是核心了,修改Launcher.java代码如下:
- package com.tutor.launcher;
- import java.util.Collections;
- import java.util.List;
- import android.app.Activity;
- import android.content.ComponentName;
- import android.content.Context;
- import android.content.Intent;
- import android.content.pm.PackageManager;
- import android.content.pm.ResolveInfo;
- import android.os.Bundle;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.AdapterView;
- import android.widget.BaseAdapter;
- import android.widget.GridView;
- import android.widget.ImageView;
- import android.widget.TextView;
- import android.widget.AdapterView.OnItemClickListener;
- public class Launcher extends Activity implements OnItemClickListener{
- private GridView mGridView;
- private Context mContext;
- private PackageManager mPackageManager;
- private List<ResolveInfo> mAllApps;
- public void onCreate(Bundle savedInstanceState) {
- super .onCreate(savedInstanceState);
- setContentView(R.layout.main);
- setupViews();
- }
- public void setupViews(){
- mContext = Launcher.this ;
- mPackageManager = getPackageManager();
- mGridView = (GridView)findViewById(R.id.allapps);
- bindAllApps();
- mGridView.setAdapter(new GridItemAdapter(mContext, mAllApps));
- mGridView.setNumColumns(4 );
- mGridView.setOnItemClickListener(this );
- }
- public void bindAllApps(){
- //这里是关键哦,我们平时写的应用总有一个activity申明成这两个属性
- //也就是应用的入口
- Intent mainIntent = new Intent(Intent.ACTION_MAIN, null );
- mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
- //符合上面条件的全部查出来,并且排序
- mAllApps = mPackageManager.queryIntentActivities(mainIntent, 0 );
- Collections.sort(mAllApps, new ResolveInfo.DisplayNameComparator(mPackageManager));
- }
- //gridview点击事件,点击进入相关应用
- @Override
- public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
- // TODO Auto-generated method stub
- ResolveInfo res = mAllApps.get(position);
- //该应用的包名和主Activity
- String pkg = res.activityInfo.packageName;
- String cls = res.activityInfo.name;
- ComponentName componet = new ComponentName(pkg, cls);
- Intent i = new Intent();
- i.setComponent(componet);
- startActivity(i);
- }
- //不明白BaseAdapter的用法 我高手进阶里有
- private class GridItemAdapter extends BaseAdapter{
- private Context context;
- private List<ResolveInfo> resInfo;
- //构造函数
- public GridItemAdapter(Context c,List<ResolveInfo> res){
- context = c;
- resInfo = res;
- }
- @Override
- public int getCount() {
- // TODO Auto-generated method stub
- return resInfo.size();
- }
- @Override
- public Object getItem( int position) {
- // TODO Auto-generated method stub
- return null ;
- }
- @Override
- public long getItemId( int position) {
- // TODO Auto-generated method stub
- return 0 ;
- }
- @Override
- public View getView( int position, View convertView, ViewGroup parent) {
- //不明白LayoutInflater的我android高手进阶里有
- convertView = LayoutInflater.from(context)
- .inflate(R.layout.application_layout, null );
- ImageView app_icon = (ImageView)convertView.findViewById(R.id.app_icon);
- TextView app_tilte = (TextView)convertView.findViewById(R.id.app_title);
- ResolveInfo res = resInfo.get(position);
- app_icon.setImageDrawable(res.loadIcon(mPackageManager));
- app_tilte.setText(res.loadLabel(mPackageManager).toString());
- return convertView;
- }
- }
- }
package com.tutor.launcher; import java.util.Collections; import java.util.List; import android.app.Activity; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; import android.content.pm.ResolveInfo; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.BaseAdapter; import android.widget.GridView; import android.widget.ImageView; import android.widget.TextView; import android.widget.AdapterView.OnItemClickListener; public class Launcher extends Activity implements OnItemClickListener{ private GridView mGridView; private Context mContext; private PackageManager mPackageManager; private List<ResolveInfo> mAllApps; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); setupViews(); } public void setupViews(){ mContext = Launcher.this; mPackageManager = getPackageManager(); mGridView = (GridView)findViewById(R.id.allapps); bindAllApps(); mGridView.setAdapter(new GridItemAdapter(mContext, mAllApps)); mGridView.setNumColumns(4); mGridView.setOnItemClickListener(this); } public void bindAllApps(){ //这里是关键哦,我们平时写的应用总有一个activity申明成这两个属性 //也就是应用的入口 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null); mainIntent.addCategory(Intent.CATEGORY_LAUNCHER); //符合上面条件的全部查出来,并且排序 mAllApps = mPackageManager.queryIntentActivities(mainIntent, 0); Collections.sort(mAllApps, new ResolveInfo.DisplayNameComparator(mPackageManager)); } //gridview点击事件,点击进入相关应用 @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // TODO Auto-generated method stub ResolveInfo res = mAllApps.get(position); //该应用的包名和主Activity String pkg = res.activityInfo.packageName; String cls = res.activityInfo.name; ComponentName componet = new ComponentName(pkg, cls); Intent i = new Intent(); i.setComponent(componet); startActivity(i); } //不明白BaseAdapter的用法 我高手进阶里有 private class GridItemAdapter extends BaseAdapter{ private Context context; private List<ResolveInfo> resInfo; //构造函数 public GridItemAdapter(Context c,List<ResolveInfo> res){ context = c; resInfo = res; } @Override public int getCount() { // TODO Auto-generated method stub return resInfo.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return null; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { //不明白LayoutInflater的我android高手进阶里有 convertView = LayoutInflater.from(context) .inflate(R.layout.application_layout, null); ImageView app_icon = (ImageView)convertView.findViewById(R.id.app_icon); TextView app_tilte = (TextView)convertView.findViewById(R.id.app_title); ResolveInfo res = resInfo.get(position); app_icon.setImageDrawable(res.loadIcon(mPackageManager)); app_tilte.setText(res.loadLabel(mPackageManager).toString()); return convertView; } } }
第五步:运行以上工程,得到的效果图如下:
图1.列出所有安装的应用.
图2.点击aQQ应用进入到相应的应用里。
OK,今天就写到这里,大家有什么不明白的地方可 以留言。thx~