开发者

Retrieve the app package and version in Android

开发者 https://www.devze.com 2023-03-26 11:26 出处:网络
I\'m implementing an error report system in my app, how can I retrieve programmatically the packa开发者_JS百科ge of my app and its version?

I'm implementing an error report system in my app,

how can I retrieve programmatically the packa开发者_JS百科ge of my app and its version?

Thanks


The PackageInfo class has a versionCode field that will give you the version, the versionName field will give you the version name, and packageName that will give you the package name. Context will give you a PackageManger instance that will give you a PackageInfo instance.

String packageName = "exampleApp";
try {
    PackageInfo pi = getApplicationContext().getPackageManager().getPackageInfo(packageName, PackageManager.GET_META_DATA);
    int versionNumber = pi.versionCode;
    String versionName = pi.versionName;
    String packageName = pi.packageName;
} catch (NameNotFoundException e) {

    e.printStackTrace();
}


Use the following:

Context.getApplicationContext.getApplicationInfo().packageName;

This returns a string.

Hope this helps!


Look at the PackageManager class.

http://developer.android.com/reference/android/content/pm/package-summary.html

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号