92

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
  • 68,613
  • 12
  • 97
  • 138
stuti
  • 981
  • 1
  • 8
  • 6

5 Answers5

209

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,530
  • 6
  • 58
  • 85
Xion
  • 21,580
  • 9
  • 51
  • 77
  • 1
    that's perfect. Been looking for it for hours, thanks Xion. :D – drulabs May 15 '12 at 19:28
  • 4
    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. – ABHIMANGAL MS 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
  • [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
45

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
  • 32,807
  • 18
  • 222
  • 281
18
 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
  • 13,304
  • 5
  • 51
  • 60
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.

-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);