简体   繁体   English

在Android中检索应用包和版本

[英]Retrieve the app package and version in Android

I'm implementing an error report system in my app, 我正在我的应用中实现错误报告系统,

how can I retrieve programmatically the package 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. PackageInfo类有一个versionCode字段,它将为您提供版本,versionName字段将为您提供版本名称,packageName将为您提供包名称。 Context will give you a PackageManger instance that will give you a PackageInfo instance. Context将为您提供一个PackageManger实例,该实例将为您提供PackageInfo实例。

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! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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