Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
481 views
in Technique[技术] by (71.8m points)

android - How to get the application name of the APK programmatically (not installed)

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...