在Dialog中显示所有应用程序,通过PackageManager获得android中的应用程序信息,然后显示在Dialog中。由于系统自带的一些应用是没有提供入口的所以我们将不能打开的应用程序过滤掉只显示能够打开的。先上张效果图:
下面来看代码:
1、获得已经安装的应用程序,并将不可打开的过滤点,只得到能打开的应用程序:
[代码]GetAllApps.java代码:
package com.mydialog;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
/**
* 获得所有应用程序
* @author kfb
*
*/
public class GetAllApps {
private Context context = null;
public GetAllApps(Context context) {
this.context = context;
}
/**
* 获得所有的APPS
* @param filter
* @return
*/
@SuppressWarnings("static-access")
public List<PackageInfo> getAllApps(int filter) {
List<PackageInfo> apps = new ArrayList<PackageInfo>();
PackageManager pManager = context.getPackageManager();
// 获取手机内所有应用
List<PackageInfo> packlist = pManager.getInstalledPackages(0);
for (int i = 0; i < packlist.size(); i++) {
PackageInfo pak = (PackageInfo) packlist.get(i);
String packageName = pak.applicationInfo.packageName;
switch (filter) {
case 0: //所有应用
if (!filterErrorPackage(pManager, packageName)) {
apps.add(pak);
}
break;
case 1: //第三方应用
if ((pak.applicationInfo.flags & pak.applicationInfo.FLAG_SYSTEM) <= 0) {
if (!filterErrorPackage(pManager, packageName)) {
apps.add(pak);
}
}
break;
case 2: //系统自带应用
if ((pak.applicationInfo.flags & pak.applicationInfo.FLAG_SYSTEM) != 0) {
if (!filterErrorPackage(pManager, packageName)) {
apps.add(pak);
}
}
break;
default:
break;
}
}
return apps;
}
/**
* 判断当前包是否可以打开
* @return
*/
public boolean filterErrorPackage(PackageManager pManager, String packageName) {
Intent intent = new Intent();
intent = pManager.getLaunchIntentForPackage(packageName);
//如果该程序不可启动(像系统自带的包,有很多是没有入口的)会返回NULL
if (intent == null) {
return true;
}
return false;
}
}
2、自定义的Dialog类,将GridView添加到Dialog中并显示应用程序列表
[代码]MyDialog.java代码:
package com.mydialog;
import java.util.ArrayList;
import java.util.List;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
/**
* 弹出所有应用列表
* @author kfb
*
*/
public class MyDialog extends Dialog implements OnItemClickListener
{
private Context context = null;
private GridView appGridView = null;
//用来记录应用程序的信息
List<AppsItemInfo> list = null;
private PackageManager pManager;
private GetAllApps AllApps= null;
private baseAdapter adapter = null;
private int filter = 0;
/**
* 显示应用程序列表的Dialog
* @param context
* @param theme
*/
public MyDialog(Context context, int filter, int theme) {
super(context, theme);
setContentView(R.layout.dialog_apps);
this.context = context;
this.filter = filter;
//设置dialog的透明度,高度和宽度
Window w = getWindow();
WindowManager.LayoutParams lp = w.getAttributes();
lp.alpha = 1.0f;//设置当前对话框的透明度
appGridView = (GridView) this.findViewById(R.id.gridview);
// 获取图片、应用名、包名
pManager = context.getPackageManager();
AllApps = new GetAllApps(context);
List<PackageInfo> appList = null;
appGridView.setNumColumns(4);
lp.width = (int)(w.getWindowManager().getDefaultDisplay().getWidth()*0.9);
appList = AllApps.getAllApps(this.filter);
w.setAttributes(lp);
list = new ArrayList<AppsItemInfo>();
for (int i = 0; i < appList.size(); i++) {
PackageInfo pinfo = appList.get(i);
AppsItemInfo shareItem = new AppsItemInfo();
// 设置图片
shareItem.setIcon(pManager.getApplicationIcon(pinfo.applicationInfo));
// 设置应用程序名字
shareItem.setLabel(pManager.getApplicationLabel(pinfo.applicationInfo).toString());
// 设置应用程序的包名
shareItem.setPackageName(pinfo.applicationInfo.packageName);
list.add(shareItem);
}
adapter = new baseAdapter();
appGridView.setAdapter(adapter);
appGridView.setOnItemClickListener(this);
// Dialog按对话框外面取消操作
this.setCanceledOnTouchOutside(true);
}
static class ViewHolder{
ImageView icon;
TextView label;
}
private class baseAdapter extends BaseAdapter {
LayoutInflater inflater = LayoutInflater.from(context);
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.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 position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder;
if (convertView == null) {
// 使用View的对象itemView与R.layout.item关联
convertView = inflater.inflate(R.layout.dialog_item, null);
holder = new ViewHolder();
holder.icon = (ImageView) convertView.findViewById(R.id.apps_image);
holder.label = (TextView) convertView.findViewById(R.id.apps_textview);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.icon.setImageDrawable(list.get(position).getIcon());
holder.label.setText(list.get(position).getLabel().toString());
return convertView;
}//end if
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
PackageManager packageManager =context.getPackageManager();
String pName = list.get(arg2).getPackageName(); //获取包名
//通过包名打开应用程序
Intent intent = new Intent();
//获取intent
intent = packageManager.getLaunchIntentForPackage(pName);
context.startActivity(intent);
}
}
[代码]AppsItemInfo.java代码:
package com.mydialog;
import android.graphics.drawable.Drawable;
/**
* 自定义一个 AppsItemInfo 类,用来存储应用程序的相关信息
* @author kfb
*
*/
public class AppsItemInfo {
private Drawable icon; // 存放图片
private String label; // 存放应用程序名
private String packageName; // 存放应用程序包名
public Drawable getIcon() {
return icon;
}
public void setIcon(Drawable icon) {
this.icon = icon;
}
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
public String getPackageName() {
return packageName;
}
public void setPackageName(String packageName) {
this.packageName = packageName;
}
}
[代码]dialog_apps.xml代码:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00000000" >
<GridView
android:id="@+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="8"
android:padding="10dip"
android:stretchMode="columnWidth"
android:gravity="center"
/>
</FrameLayout>
5、注意android:maxEms="6"表示最大只能显示6个汉字,由于一个汉字由两个字节组成而字符是一个字节所以可以最大显示12个字符。
[代码]dialog_item.xml代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:descendantFocusability="blocksDescendants">
<ImageView
android:id="@+id/apps_image"
android:layout_width="480dip"
android:layout_height="48dip"
android:paddingTop="10dip"
android:icon="@drawable/ic_launcher" />
<TextView
android:id="@+id/apps_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="1"
android:maxEms="6"
android:textColor="#ffffff"
android:paddingBottom="10dip"
android:gravity="center" />
</LinearLayout>
第一次写博客也不知道写什么好!那就只能上一堆的代码了,说到底还只是个程序员啊!一点文学细胞都没有。。。。
源码下载: