简体   繁体   中英

How to get package name from apk in Android?

I am developing an app which will contain a list of Apps. On click the user will be redirected to the Play Store to download this app. On successful download I have to send that apps package name to a server to validate it. How can I do that?

You can get all installed app in device using below code

final PackageManager pm = getPackageManager();
//get a list of installed apps.
List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);

for (ApplicationInfo packageInfo : packages) {
    Log.d(TAG, "Installed package :" + packageInfo.packageName);
    Log.d(TAG, "Source dir : " + packageInfo.sourceDir);
    Log.d(TAG, "Launch Activity :" + pm.getLaunchIntentForPackage(packageInfo.packageName)); 
}

and find package name in for loop.

I assume you want to do this at runtime, so your app can read its own package_id w/o having this hardcoded. For that you need to use PackageManager 's getPackageInfo() method:

protected String getPackageName() {
    try {
        PackageInfo packageInfo = getPackageManager.getPackageInfo(getPackageName(), 0);

        return packageInfo.applicationInfo.packageName;
    } catch (Exception e) {
        e.printStacktrace();
    }

    return null;
}

使用apkanalyzer ,它是 Android 工作室的一部分:

apkanalyzer manifest application-id path/to/apk/file.apk

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM