How to get package name from apk in Android?

后端 未结 3 964
长情又很酷
长情又很酷 2021-01-29 08:42

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

3条回答
  •  北恋
    北恋 (楼主)
    2021-01-29 09:33

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

提交回复
热议问题