从包名中获取应用程序信息



我有一个应用程序包名。有没有办法得到这个应用程序的图标,名称?包名示例:com.example.name

试试这样写:

    try {
        ApplicationInfo app = this.getPackageManager().getApplicationInfo("com.example.name", 0);        
        Drawable icon = packageManager.getApplicationIcon(app);
        String name = packageManager.getApplicationLabel(app);
        return icon;
    } catch (NameNotFoundException e) {
        Toast toast = Toast.makeText(this, "error in getting icon", Toast.LENGTH_SHORT);
        toast.show();
        e.printStackTrace();
    }
String PackgeNameApp = "com.test.icon";
            String name="NULL";
            try
            {
                ApplicationInfo app = this.getPackageManager().getApplicationInfo(PackgeNameApp, 0);
                Drawable icon = getPackageManager().getApplicationIcon(app);
                 name = getPackageManager().getApplicationLabel(app).toString();
            }
            catch (PackageManager.NameNotFoundException e)
            {
                //
            }
Drawable icon =getPackageManager().getApplicationIcon("ur_package");
Strign label = context.getPackageManager().getApplicationLabel("ur_package");

如果你想获得安装应用程序的包名,那么这段代码将对你有帮助。

 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
                mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
                List<ResolveInfo> pkgAppsList = MainActivity.this.getPackageManager().queryIntentActivities( mainIntent, 0);
                Log.i("InstalledAPK",pkgAppsList.toString());

              for(int i=0;i<pkgAppsList.size();i++){
                  list.add(pkgAppsList.get(i).toString());
                  Log.e("PACKAGENAME",""+MainActivity.this.getPackageManager().getInstalledPackages(i).get(i).packageName);
              }

                ArrayAdapter<String> adapter=new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,list);
                listView.setAdapter(adapter);

最新更新