96

I want to get the application name from application package name. Somebody please show me how I can get this.

public class AppInstalledListener extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        String action = intent.getAction();
        if(action.equals("android.intent.action.PACKAGE_ADDED")){
            Logger.debug("DATA:"+intent.getData().toString());
        }
        if(action.equals("android.intent.action.PACKAGE_REMOVED")){
            Logger.debug("DATA:"+intent.getData().toString());
        }
        if(action.equals("android.intent.action.PACKAGE_REPLACED")){
            Logger.debug("DATA:"+intent.getData().toString());
        }
    }
}
Jerry
  • 70,495
  • 13
  • 100
  • 144
stuti
  • 1,021
  • 1
  • 9
  • 6

6 Answers6

217

You can use PackageManager class to obtain ApplicationInfo:

final PackageManager pm = getApplicationContext().getPackageManager();
ApplicationInfo ai;
try {
    ai = pm.getApplicationInfo( this.getPackageName(), 0);
} catch (final NameNotFoundException e) {
    ai = null;
}
final String applicationName = (String) (ai != null ? pm.getApplicationLabel(ai) : "(unknown)");

This would return the application name as defined in <application> tag of its manifest.

Mike Mackintosh
  • 13,917
  • 6
  • 60
  • 87
Xion
  • 22,400
  • 10
  • 55
  • 79
  • 1
    that's perfect. Been looking for it for hours, thanks Xion. :D – drulabs May 15 '12 at 19:28
  • 6
    its returning me unknown all the time i alrady have package name just want to know name from package name – nida Feb 12 '15 at 15:54
  • This is not working when i use this in the global receiver – Ziv Kesten Nov 01 '15 at 09:45
  • you cannot get applicationInfo of a package after PACKAGE_REMOVED. – John61590 Jul 18 '18 at 19:06
  • perfect one.. just copy paste.. gets the work done. thank you. – user7418129 Mar 13 '20 at 05:35
  • This logic will not work in Android 11 onwards, You need to have permissions. Did anyone find any other way to handle this? – Tejas Feb 17 '22 at 09:31
  • 2
    [Be carefull as you will need to use `` in manifest for Android 11 (API 30) onwards.](https://developer.android.com/training/package-visibility) If you want to query about any package, you can instead use the permission `android.permission.QUERY_ALL_PACKAGES`. – Mereo4 Feb 24 '22 at 15:51
46

Try this

final String packageName = "my.application.package"
PackageManager packageManager= getApplicationContext().getPackageManager();
String appName = (String) packageManager.getApplicationLabel(packageManager.getApplicationInfo(packageName, PackageManager.GET_META_DATA));

and replace packageName with your package full name.

you can get packageName using mContext.getPackageName() where mContext = yourActivityName.this for Activity and getActivity() for fragment.

Zar E Ahmer
  • 33,936
  • 20
  • 234
  • 300
22
 public static String getAppNameFromPkgName(Context context, String Packagename) {
    try {
        PackageManager packageManager = context.getPackageManager();
        ApplicationInfo info = packageManager.getApplicationInfo(Packagename, PackageManager.GET_META_DATA);
        String appName = (String) packageManager.getApplicationLabel(info);
        return appName;
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
        return "";
    }
}
Nikunj Paradva
  • 15,332
  • 5
  • 54
  • 65
3

It seems that you are able to receive the event of new package added after that its a very simple concept to get the all relevant information about that package like one such information is the application name so here is the concept

-> your device package manager has all the information related to it so just make an object of that it will give you all the information related with the package name.

-> You should also remember that the intent gives you "package: real_package_name" so first you have to get real name first by spilling(I used) or by any other simple implementation of String

-> Here is the code hope you will get what you want I m also giving information about how you can get app name,app icon,app version,app version code etc.....

    public class NewAppReciver extends BroadcastReceiver{

@Override
public void onReceive(Context context, Intent intent) {
    if(intent.getAction().equals("android.intent.action.PACKAGE_ADDED")){
    String[] a=intent.getData().toString().split(":");
    String packageName=a[a.length-1];

    List<PackageInfo> packageInfoList =          context.getPackageManager().getInstalledPackages(0);
    for (int i = 0; i < packageInfoList.size(); i++) {
        PackageInfo packageInfo = packageInfoList.get(i);
        if(packageInfo.packageName.equals(packageName)){
            String appName = packageInfo.applicationInfo.loadLabel(context.getPackageManager()).toString();
            String appVersion = packageInfo.versionName;
            int appVerCode = packageInfo.versionCode;
            Drawable app_icon = packageInfo.applicationInfo.loadIcon(context.getPackageManager());
             }
         }
      }
   }    
}

But at the time of the application Uninstall you can only get the package name as on Un installation all other information gets removed by the system.

0

Try this below, it worked for me.

public static ArrayList<ApplicationInfo> getLaunchableInstalledApps(Context context){

    final PackageManager pm = context.getPackageManager();
    Intent intent = new Intent(Intent.ACTION_MAIN, null);
    intent.addCategory(Intent.CATEGORY_LAUNCHER);
    ArrayList<ApplicationInfo> list = new ArrayList<>();
    for (ResolveInfo resolveInfo:pm.queryIntentActivities(intent, PackageManager.GET_META_DATA)){
        try {
            ApplicationInfo app = context.getPackageManager().getApplicationInfo(resolveInfo.activityInfo.packageName, 0);
            list.add(app);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    if (cacheAppsList.isEmpty()){
        cacheAppsList.addAll(list);
    }

    return list;
}
-2
PackageManager pm = getPackageManager();

IntentFilter filter = new IntentFilter();
filter.addAction("android.intent.action.MAIN");
filter.addCategory("android.intent.category.HOME");
filter.addCategory("android.intent.category.DEFAULT");

Context context = getApplicationContext();
ComponentName component = new ComponentName(context.getPackageName(), TestReplaceHomeAppActivity.class.getName());

ComponentName[] components = new ComponentName[] {new ComponentName("com.android.launcher", "com.android.launcher.Launcher"), component};

pm.clearPackagePreferredActivities("com.android.launcher");
pm.addPreferredActivity(filter, IntentFilter.MATCH_CATEGORY_EMPTY, components, component);