3

Based How to get the name of the application in android?.

I have this snippet. `andPath| is the location of the APK.

PackageInfo info = getPackageManager().getPackageArchiveInfo(andPath, 0);
ApplicationInfo appinfo = info.applicationInfo;
String packageName = getPackageManager().getApplicationLabel(appinfo).toString();

PackageManager pm=getPackageManager();
ApplicationInfo ai;
try {
     ai = pm.getApplicationInfo(packageName, 0);
} catch (final NameNotFoundException e) {
     ai = null;
}

final String applicationName = (String) (ai != null ? pm.getApplicationLabel(ai) : "(unknown)");

applicationName returns me the package name.

CharSequence c = pm.getApplicationLabel(pm.getApplicationInfo(info.processName,PackageManager.GET_META_DATA));

c returns me null.

Does anyone know what I'm missing here?

Community
  • 1
  • 1
fwoop
  • 75
  • 1
  • 3
  • 7

5 Answers5

8

This is how you can get the application label from not yet installed APK file:

public static String getAppLabel(PackageManager pm, String pathToApk) {  
    PackageInfo packageInfo = pm.getPackageArchiveInfo(pathToApk, 0);

    if (Build.VERSION.SDK_INT >= 8) {
        // those two lines do the magic:
        packageInfo.applicationInfo.sourceDir = pathToApk;
        packageInfo.applicationInfo.publicSourceDir = pathToApk;
    }

    CharSequence label = pm.getApplicationLabel(packageInfo.applicationInfo);
    return label != null ? label.toString() : null;
}

No need to patch the applicationInfo prior to SDK 8. See this issue for more details.

Alex Lipov
  • 13,503
  • 5
  • 64
  • 87
  • I think this answer should be accepted as the correct answer, since it perfectly suits the answer for the question. – Sandeep R Jan 18 '17 at 08:09
4

if you have a problem here i post the full code to get the application name

its returns ApplicationInfo

public List<ApplicationInfo> getApplicationList(Context con){
        PackageManager p = con.getPackageManager(); 
        List<ApplicationInfo> info = p.getInstalledApplications(0);
        return info;
    }

it returns ApplicationName

public String applicationLabel(Context con,ApplicationInfo info){
        PackageManager p = con.getPackageManager();
        String label = p.getApplicationLabel(info).toString();
        return label;
    }

on your onCreate function paste the below code

List<ApplicationInfo> info = new ArrayList<ApplicationInfo>();

        info = list.getApplicationList(this);
        LinearLayout layout = (LinearLayout)findViewById(R.id.content);
        layout.setOrientation(1);
        int size = info.size();
        Log.d("size",String.valueOf(size));
        for(int i=0;i<size;i++){
            Log.d("Val of I",String.valueOf(i));
            String appname = list.applicationLabel(this,info.get(i)).toString();
            Log.d("AppName",appname);
            TextView tv = new TextView(this);
            tv.setText(appname);
            layout.addView(tv);
        }

    }
Bucks
  • 689
  • 3
  • 11
  • 28
  • list is a object of class which contains getApplicationList and applicationLabel functions – Bucks Sep 11 '12 at 04:37
  • but this is getting the application name based on the installed app right? is it possible to get the application name based only on the APK which is not installed? thanks for the reply btw! – fwoop Sep 11 '12 at 05:12
  • Application name which is not installed means you want to get list of apks present in sd card ? – Bucks Sep 11 '12 at 05:20
  • uhm no, i can retrieve the apks but what im trying to get is the application name of that specific apk without installing, is this possible? thanks! – fwoop Sep 11 '12 at 05:27
  • 1
    i think its not possible to get application info without installing by installing that apk you can get – Bucks Sep 11 '12 at 05:40
  • @Bucks: Sorry again, but what is 'list'? There is a "}" not needed at the end. – Luis A. Florit Nov 13 '12 at 14:23
0
public String applicationLabel (ApplicationInfo info){
     PackageManager p = con.getPackageManager();
     return p.getApplicationLabel(info).toString();
}

I hope you got application info pass that application info to this function it will give application name.

afollestad
  • 2,929
  • 5
  • 30
  • 44
Bucks
  • 689
  • 3
  • 11
  • 28
0
String title = getResources().getString(R.string.app_name);
Marko
  • 20,385
  • 13
  • 48
  • 64
Axay Prajapati
  • 791
  • 5
  • 20
0

Simply use regular expression to get the name and compare with the app Label.

Alok Singh
  • 35
  • 10
  • This was published as an answer, but possibly it should be a comment, otherwise please edit your answer and add an explanation that may be useful. – borchvm Feb 11 '20 at 07:32