使用PackageManager获取包名称

时间:2016-01-13 09:02:59

标签: android listview

我的listview显示已安装的应用程序具有互联网权限,但它工作正常,我试图启用点击列表项并将用户带到他们可以卸载应用程序的屏幕,我的代码工作没有任何强制关闭但打开卸载屏幕只有0.25秒,然后返回。这是因为它没有获得正确的包名称。我该如何解决这个问题?

    public class MainActivity extends Activity {

        ArrayList<String> results = new ArrayList<String>();

         @Override
           protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
              getInstalledApps(this);
              ArrayAdapter adapter = new ArrayAdapter<String>(this, R.layout.activity_listview, results);

              ListView listView = (ListView) findViewById(R.id.mobile_list);
              listView.setAdapter(adapter);

              listView.setOnItemClickListener(new OnItemClickListener() {
                  @Override
                  public void onItemClick(AdapterView<?> parent, View view, int position,
                          long id) {
                      String packageName = results.get(position);
                      Intent intent = new Intent(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                      intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                      intent.setData(Uri.parse("package:" + packageName));
                      startActivity(intent);
                  }
              });
         }

          private ArrayList<String> getInstalledApps(Context context) {
            PackageManager packageManager = context.getPackageManager();
            List<PackageInfo> applist = packageManager.getInstalledPackages(0);
            Iterator<PackageInfo> it = applist.iterator();
            while (it.hasNext()) {
                PackageInfo pk = (PackageInfo) it.next();
                if ((pk.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
                    Log.v("system app using internet = ", ""+pk.applicationInfo.loadLabel(packageManager));
                    continue;
                }

                if (PackageManager.PERMISSION_GRANTED == packageManager .checkPermission(Manifest.permission.INTERNET, pk.packageName) ||
                        PackageManager.PERMISSION_GRANTED == packageManager .checkPermission(Manifest.permission.READ_EXTERNAL_STORAGE, pk.packageName))
                    results.add("" + pk.applicationInfo.loadLabel(packageManager));
            }

            Log.v("app using internet = ", results.toString());

            return results;
        }
    }

我也想将其直接带到卸载屏幕,而不是将其带到应用信息屏幕。

2 个答案:

答案 0 :(得分:0)

您正在结果数组中存储应用名称,而不是包名称,这是不同的。

以下是一个示例:Facebook应用的名称为Facebook,但其包名为com.facebook.katana

查询PackageManager时,您不仅要求pk.applicationInfo.loadLabel(packageManager),还要pk.packageName(例如,在另一个数组中)。

因此,您的代码应按以下方式进行编辑:

  1. ArrayList<String> results = new ArrayList<String>();添加ArrayList<String> packageNames = new ArrayList<String>();

  2. results.add("" + pk.applicationInfo.loadLabel(packageManager));下添加packageNames.add(pk.packageName);if (...)使用括号!

  3. String packageName = results.get(position);替换为String packageName = packageNames.get(position);

答案 1 :(得分:0)

    Try this
    ArrayList<AppInfodata> appsDataList = new ArrayList<>();
    public void getAllApps() {

            PackageManager packageManager = getPackageManager();
            List apps = packageManager
                    .getInstalledPackages(PackageManager.SIGNATURE_MATCH);
            if (apps != null && !apps.isEmpty()) {

                for (int i = 0; i < apps.size(); i++) {
                    PackageInfo p = (PackageInfo) apps.get(i);
                    ApplicationInfo appInfo = null;
                    try {
                        appInfo = packageManager.getApplicationInfo(p.packageName,
                                0);

                        AppInfodata app = new AppInfodata();
                        app.appname = p.applicationInfo.loadLabel(packageManager)
                                .toString();
                        app.packagename = p.packageName;

                      app.icon = p.applicationInfo.loadIcon(packageManager);


                            appsDataList.add(app);
                        }

                    } catch (NameNotFoundException e) {
                        e.printStackTrace();
                    }
                }

                // sort the list of applications alphabetically

            }
            public class AppInfodata {
        String appname;
        String packagename;

        Drawable icon;


    }

class AppListAdapter extends ArrayAdapter<AppInfoData>
{
AppListAdapter(Context context,int resource,ArrayList<AppInfoData> datalist)
{
super(context,resource,datalist);
}
getView(.....)
{
AppInfoData data =getItem(position);
}

}