在android中实现一个像桌面那样把所有应用都列举出来。用GridView来实现显示。
主要代码如下:
先修改main.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" > <GridView android:id="@+id/gridView" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>
然后创建一个xml文件来显示你想在GridView中显示的格式,我这边是用一个图标加一个文件来显示的。
application.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/icon" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
package com.mzz; 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 ListApplications extends Activity implements OnItemClickListener{ private GridView mGridView = null ; private PackageManager mPackageManager = null ; private List<ResolveInfo> mAllApps = null ; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); showView(); } public void showView() { mPackageManager = getPackageManager(); mGridView = (GridView)findViewById(R.id.gridView); getAllApps(); mGridView.setAdapter(new MyAdapter(ListApplications.this , mAllApps)); mGridView.setNumColumns(3);//每行显示3个 mGridView.setOnItemClickListener(this); } public void getAllApps() { //设置一个Intent的action和category为需要显示 Intent intent = new Intent(Intent.ACTION_MAIN , null); intent.addCategory(Intent.CATEGORY_LAUNCHER); //如何条件的查出来 mAllApps = mPackageManager.queryIntentActivities(intent, 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 mComponentName = new ComponentName(pkg, cls); Intent intent = new Intent(); intent.setComponent(mComponentName); startActivity(intent); } //Adapter class MyAdapter extends BaseAdapter { private Context mContext ; private List<ResolveInfo> mAllApps ; MyAdapter(Context context , List<ResolveInfo> allApps) { mContext = context ; mAllApps = allApps ; } @Override public int getCount() { // TODO Auto-generated method stub return mAllApps.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) { // TODO Auto-generated method stub convertView = LayoutInflater.from(mContext).inflate(R.layout.applications, null); ImageView icon = (ImageView)convertView.findViewById(R.id.icon); TextView title = (TextView)convertView.findViewById(R.id.title); ResolveInfo res = mAllApps.get(position); icon.setImageDrawable(res.loadIcon(mPackageManager)); title.setText(res.loadLabel(mPackageManager)); return convertView; } } }
到这里代码的全部编写完成了。
效果如下图所见:
虽然和本身的显示相比,我的这个有点丑。没有处理图标的大小,不过功能上实现了。当你点击相应的图标的时候,会进入相应的应用的主Activity。
呵呵。。。