Android 尝试获取包版本信息以显示在“关于”消息中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2610473/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Trying to get Package Version Information to display in an "About" message
提问by user313135
I have been struggling with getting the versionName for a running application from the PackageInfo Object type.
我一直在努力从 PackageInfo 对象类型获取正在运行的应用程序的 versionName。
I have constructed a Parcelable Interface with all of the fields associated with the PackageInfo Object type. The primary input for that interface method is a Parcel object.
我已经构建了一个 Parcelable 接口,其中包含与 PackageInfo 对象类型关联的所有字段。该接口方法的主要输入是 Parcel 对象。
I cannot seem to figure out how to correctly associate these Parcelable, Parcel, and PackageInfo objects.
我似乎无法弄清楚如何正确关联这些 Parcelable、Parcel 和 PackageInfo 对象。
Is there any sample code out there that I can look at? Doesn't seem like it should be that difficult but it seems to be stumping me.
有没有我可以查看的示例代码?看起来应该没有那么难,但它似乎让我很难过。
Thanks Jazz
谢谢爵士
回答by Casebash
Sample code:
示例代码:
try {
PackageInfo packageInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
return packageInfo.versionName;
} catch (NameNotFoundException e) {
//Handle exception
}
回答by KimKha
Modern way:
现代方式:
If you build your Android project by Gradle (with Android Studio), there is a best shortcut to get version:
如果您通过 Gradle(使用 Android Studio)构建您的 Android 项目,则有一个获取版本的最佳快捷方式:
com.yourproject.BuildConfig.VERSION_NAME
It's linked to versionName
in your build.gradle
file.
它链接到versionName
您的build.gradle
文件中。
In fact, you can get Application ID, and more information from your build.gradle
file by this way.
事实上,您可以build.gradle
通过这种方式从您的文件中获取应用程序 ID 和更多信息。
回答by Janusz
I use the following code to get the version used in the manifest. I wrote a small function to encapsulate and hide this somewhat big chunk needed to just get an int.
我使用以下代码来获取清单中使用的版本。我写了一个小函数来封装和隐藏这个获取 int 所需的大块。
private int getVersion() {
int version = -1;
try {
PackageInfo pInfo = getPackageManager().getPackageInfo(getPackageName(), PackageManager.GET_META_DATA);
version = pInfo.versionCode;
} catch (NameNotFoundException e1) {
Log.e(this.getClass().getSimpleName(), "Name not found", e1);
}
return version;
}
This will return the int chosen to identify your version in the market (VersionCode). Not the VersionName. To see how to change to code for this have a look at the code from Casebash.
这将返回选择用于标识您在市场上的版本的 int (VersionCode)。不是版本名称。要了解如何为此更改代码,请查看 Casebash 中的代码。
回答by CommonsWare
PackageInfo
's versionName
is a public data member. You do not need to do anything special to get "the versionName for a running application from the PackageInfo Object type". If you have a PackageInfo
object named info
, you access versionName
via info.versionName
.
PackageInfo
'sversionName
是公共数据成员。您无需执行任何特殊操作即可从 PackageInfo 对象类型获取“正在运行的应用程序的 versionName”。如果您有一个PackageInfo
名为的对象info
,您可以versionName
通过info.versionName
.
回答by Ferlle G.
Also there is a shortcut:
还有一个捷径:
String version = new PackageInfo().versionName;
String version = new PackageInfo().versionName;