1

我想要从 google play 或任何外部存储安装的应用程序列表,而不是预安装的系统应用程序。

我只是使用下面的代码.....

 class PInfo {
    private String appname = "";
    private String pname = "";
    private String versionName = "";
    private int versionCode = 0;
    private Drawable icon;
    private void prettyPrint() {
        Log.v("", "*** appname = " + appname + " *** \n" + " packagename = " + pname + "\n" + " version name =  " + versionName + "\n" + " version code = " + versionCode + " \n\n\n\n");
    }
}

private ArrayList<PInfo> getPackages() {
    ArrayList<PInfo> apps = getInstalledApps(true); /* false = no system packages */
    final int max = apps.size();
    int i = 0; 
    for (i=0; i<max; i++) {
        apps.get(i).prettyPrint();
    }
    Log.v(TAG, "Total APPs = "+i);
    return apps;
}

private ArrayList<PInfo> getInstalledApps(boolean getSysPackages) {
    ArrayList<PInfo> res = new ArrayList<PInfo>();        
    List<PackageInfo> packs = getPackageManager().getInstalledPackages(PackageManager.GET_META_DATA);
    for(int i=0;i<packs.size();i++) {
        PackageInfo p = packs.get(i);
        if ((!getSysPackages) && (p.versionName == null)) {
            continue ;
        }
        PInfo newInfo = new PInfo();
        newInfo.appname = p.applicationInfo.loadLabel(getPackageManager()).toString();
        newInfo.pname = p.packageName;
        newInfo.versionName = p.versionName;
        newInfo.versionCode = p.versionCode;
        newInfo.icon = p.applicationInfo.loadIcon(getPackageManager());
        res.add(newInfo);
    }
    return res; 
}

但是这段代码也给了我系统应用程序的信息.....

任何人都可以知道如何只获取已安装的应用程序,除了已安装的系统应用程序吗?

谢谢....

4

2 回答 2

3

要发现您的应用程序是否是系统应用程序,您可以使用以下代码:

PackageManager pm = getPackageManager();
List<ApplicationInfo> installedApps = pm.getInstalledApplications(0);

for (ApplicationInfo aInfo: installedApps) {

    if ((aInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
        // system application
    } else {
        //user application
    }
}

如果您需要发现应用程序是否从市场安装,您需要使用以下Package: 方法getInstallerPackageName。它将返回安装您的应用程序的应用程序的 packageName。

于 2012-11-02T14:23:21.843 回答
0
public class AppList extends Activity {
 private ListView lView;
 private ArrayList results = new ArrayList();

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  lView = (ListView) findViewById(R.id.list1);
  PackageManager pm = this.getPackageManager();

  Intent intent = new Intent(Intent.ACTION_MAIN, null);
  intent.addCategory(Intent.CATEGORY_LAUNCHER);

  List list = pm.queryIntentActivities(intent, PackageManager.PERMISSION_GRANTED);
  for (ResolveInfo rInfo : list) {
   results.add(rInfo.activityInfo.applicationInfo
     .loadLabel(pm).toString());
   Log.w("Installed Applications", rInfo.activityInfo.applicationInfo
     .loadLabel(pm).toString());
  } 
  lView.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, results));
    }
}
于 2012-11-02T12:46:18.917 回答