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

6 Answers 6

221

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.

Sign up to request clarification or add additional context in comments.

7 Comments

that's perfect. Been looking for it for hours, thanks Xion. :D
its returning me unknown all the time i alrady have package name just want to know name from package name
This is not working when i use this in the global receiver
you cannot get applicationInfo of a package after PACKAGE_REMOVED.
Be carefull as you will need to use <queries> in manifest for Android 11 (API 30) onwards. If you want to query about any package, you can instead use the permission android.permission.QUERY_ALL_PACKAGES.
|
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.

Comments

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 "";
    }
}

Comments

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.

1 Comment

intent.getData().toString() is not the way to go, the data in the intent is structured.
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;
}

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.