我想获得Minecraft Pocket Edition的版本名称(例如0.13.0.b5)。
这是MCPE(Minecraft Pocket Edition)的软件包
com.mojang.minecraftpe
我想将版本发送到String
。我怎样才能做到这一点?
获取包名称的代码:
List<PackageInfo> packages = getPackageManager().getInstalledPackages(0);
PackageInfo mypackage = <com.mojang.minecraftpe>;
String versionName = mypackage.versionName;
解决方案:
try
{
PackageInfo eInfo = getPackageManager().getPackageInfo("com.mojang.minecraftpe", 0);
changelog=eInfo.versionName;
lol.setText(changelog);
}
catch (PackageManager.NameNotFoundException e)
{}
答案 0 :(得分:13)
它从包管理器(通过包)获取包信息并返回版本名称。:
PackageInfo packageInfo = getPackageManager().getPackageInfo("com.mojang.minecraftpe", 0);
return packageInfo.versionName;
答案 1 :(得分:2)
List<PackageInfo> apps = getPackageManager().getInstalledPackages(0);
ArrayList<App> result = new ArrayList<>();
for(int i=0;i<apps.size();i++) {
PackageInfo p = apps.get(i);
App newApp = new App();
newApp.pname = p.packageName;
newApp.versionName = p.versionName;
newApp.versionCode = p.versionCode;
}
String minecraft = "com.mojang.minecraftpe";
for (App name : result) {
if (Objects.equals(minecraft, name.pname))
String versionName = name.versionName;
}
// -----类描述------
class App {
String pname = "";
String versionName = "";
int versionCode = 0;
}
如果您需要版本代码,请修改&#39; if&#39;使用name.VersionCode进行for循环的条件。
参考:PackageManager(Class for retrieving various kinds of information related to the application packages that are currently installed on the device.)