Android: how to get the name of apk file programmatically?

后端 未结 4 1281
失恋的感觉
失恋的感觉 2020-12-10 16:37

For some reasons, my app can be installed with different apk names. At launch, I would like to know what is the name of the apk file. Do you know how to do that ?

Th

4条回答
  •  星月不相逢
    2020-12-10 17:06

    The apk or app name can be retrieved using package manager. According to alex's answer here - https://stackoverflow.com/a/30348666/2026280

    Assuming you have the path of the 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;
    }
    

提交回复
热议问题