请问在Android开发中如何只获得手机桌面上的App列表

我现在能够获得手机中安装的所有App列表,但我只想获得手机桌面上的App列表,请问应该如何解决?

2个回答

第一步:建立基本数据:PakageMod.java

public class PakageMod
{

public String pakageName;//应用的包名
public String appName;//应用的名称
public Drawable icon;//应用图标

public PakageMod() {
super();
}

public PakageMod(String pakageName, String appName, Drawable icon) {
super();
this.pakageName = pakageName;
this.appName = appName;
this.icon = icon;
}
}
第二步:建立适配器
public class DemoGridAdapter extends BaseAdapter {

private LayoutInflater inflater;
private List datas;

public DemoGridAdapter(Context context, List datas) {
super();
inflater = LayoutInflater.from(context);
this.datas = datas;
}

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

@Override
public Object getItem(int position) {
return null;
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
// 使用View的对象itemView与R.layout.item关联
convertView = inflater.inflate(R.layout.apps, 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(datas.get(position).icon);
holder.label.setText(datas.get(position).appName);

return convertView;

}

class ViewHolder {
private ImageView icon;
private TextView label;
}
}
第三步:建立适配器的视图:
<?xml version="1.0" encoding="utf-8"?>
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical" >

android:id="@+id/apps_image"
android:layout_width="48dip"
android:layout_height="48dip"
android:icon="@drawable/ic_launcher" />

android:id="@+id/apps_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxEms="5"
android:maxLines="1"
android:text="good" />


第四步:在Activity中获取到列表并显示到GridView中,并点击进入应用
public class PackageInfoDemo extends Activity {

private GridView gridview;
private PackageManager pManager;
private List datas;
private String tag = "MainActivity";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_main);
// 取得gridview
gridview = (GridView) findViewById(R.id.gridview);
// 获取图片、应用名、包名
pManager = PackageInfoDemo.this.getPackageManager();
List appList = getAllApps(PackageInfoDemo.this);
datas = new ArrayList();
for (int i = 0; i < appList.size(); i++) {
PackageInfo pinfo = appList.get(i);
PakageMod shareItem = new PakageMod();
// 设置图片
shareItem.icon = pManager.getApplicationIcon(pinfo.applicationInfo);
// 设置应用程序名字
shareItem.appName = pManager.getApplicationLabel(
pinfo.applicationInfo).toString();
// 设置应用程序的包名
shareItem.pakageName = pinfo.applicationInfo.packageName;

  datas.add(shareItem);

}
gridview.setAdapter(new baseAdapter(this, datas));

// 点击应用图标时,做出响应
gridview.setOnItemClickListener(new ClickListener());

}

public static List getAllApps(Context context) {

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);
  // if()里的值如果<=0则为自己装的程序,否则为系统工程自带
  if ((pak.applicationInfo.flags & pak.applicationInfo.FLAG_SYSTEM) <= 0) {
    // 添加自己已经安装的应用程序
    // apps.add(pak);
  }
  apps.add(pak);
}
return apps;

}

private class ClickListener implements OnItemClickListener {

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
    long arg3) {
  Intent intent = new Intent();
  intent = PackageInfoDemo.this.getPackageManager()
      .getLaunchIntentForPackage(datas.get(position).pakageName);
  startActivity(intent);
}

}
}
public class ResolveInfoDemo extends Activity {
private GridView gridview;
private PackageManager pManager;
private List datas;
private String tag = "ResolveInfoDemo";

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_main);
// 取得gridview
gridview = (GridView) findViewById(R.id.gridview);

// 获取图片、应用名、包名
pManager = this.getPackageManager();

datas = new GetAllApps(this).getDatas();
gridview.setAdapter(new DemoGridAdapter(this, datas));
gridview.setOnItemClickListener(new ClickListener());

}

// 当用户点击应用程序图标时,将对这个类做出响应
private class ClickListener implements OnItemClickListener {

@Override
public void onItemClick(AdapterView<?> adapterView, View view, int arg2,
    long arg3) {
  Intent intent = new Intent();
  intent = getPackageManager().getLaunchIntentForPackage(
      datas.get(arg2).pakageName);
  startActivity(intent);
}

}
}public class GetAllApps {

private Context mContext;
private PackageManager packageManager;
private int mIconDpi;
private String tag = "GetAllApps";
private List datas = new ArrayList();

public GetAllApps(Context mContext){
this.mContext = mContext;
ActivityManager activityManager =
(ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
packageManager = mContext.getPackageManager();
mIconDpi = activityManager.getLauncherLargeIconDensity();
}

public void loadAllAppsByBatch() {
List apps = null;
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
apps = packageManager.queryIntentActivities(mainIntent, 0);
for (int i = 0; i < apps.size(); i++) {
String packageName = apps.get(i).activityInfo.applicationInfo.packageName;
String title = apps.get(i).loadLabel(packageManager).toString();
Drawable icon = null;
if(title == null){
title = apps.get(i).activityInfo.name;
}
ActivityInfo info = apps.get(i).activityInfo;
icon = getFullResIcon(info);
datas.add(new PakageMod(packageName,title,icon));
}
}

public Drawable getFullResIcon(ActivityInfo info) {
Resources resources;
try {
resources = packageManager.getResourcesForApplication(
info.applicationInfo);
} catch (PackageManager.NameNotFoundException e) {
resources = null;
}
if (resources != null) {
int iconId = info.getIconResource();

if (iconId != 0) {
return getFullResIcon(resources, iconId);
}
}
return getFullResDefaultActivityIcon();
}

public Drawable getFullResDefaultActivityIcon() {
return getFullResIcon(Resources.getSystem(),
android.R.mipmap.sym_def_app_icon);
}

public Drawable getFullResIcon(Resources resources, int iconId) {
Drawable d;
try {
// requires API level 15 (current min is 14):
d = resources.getDrawableForDensity(iconId, mIconDpi);
} catch (Resources.NotFoundException e) {
d = null;
}

return (d != null) ? d : getFullResDefaultActivityIcon();
}

public List getDatas() {
loadAllAppsByBatch();
return datas;
}
}
这里getDrawableForDensity需要是15版本以上的SDK支持,所有低版本的Launcher不可以使用;

本博文使用的两种方法都不需要配置任何权限;

Android4.2的Launcher中使用ResolveInfo进行获取所用的应用列表,这里的ResolveInfo的Demo也是从Launcher源码中抄出来的

haizeiwangjoin
haizeiwangjoin 我是用ListView显示的,当加上下面的语句时,虽然手机系统中的一些包程序不显示了,但像图库,日历这些自带的程序也没了。但如果没有下面的语句,手机中所有的程序都会显示,例如Android系统,网络位置等等。想问一下能不能既把图库,日历这些程序也显示在列表中,而且不让Android系统,网络位置这些根本用户不会接触的程序显示。也就是说能不能只显示手机界面上的APP? PackageInfo pak = (PackageInfo) packlist.get(i); // if()里的值如果<=0则为自己装的程序,否则为系统工程自带 if ((pak.applicationInfo.flags & pak.applicationInfo.FLAG_SYSTEM) <= 0) {
5 年多之前 回复

我是用ListView显示的,当加上下面的语句时,虽然手机系统中的一些包程序不显示了,但像图库,日历这些自带的程序也没了。但如果没有下面的语句,手机中所有的程序都会显示,例如Android系统,网络位置等等。想问一下能不能既把图库,日历这些程序也显示在列表中,而且不让Android系统,网络位置这些根本用户不会接触的程序显示。也就是说能不能只显示手机界面上的APP?
PackageInfo pak = (PackageInfo) packlist.get(i);
// if()里的值如果<=0则为自己装的程序,否则为系统工程自带
if ((pak.applicationInfo.flags & pak.applicationInfo.FLAG_SYSTEM) <= 0) {

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问