我需要弄清楚如何获取或制作Android应用程序的版本号。我需要在UI中显示内部版本号。
我必须使用AndroidManifest.xml吗?
我需要弄清楚如何获取或制作Android应用程序的版本号。我需要在UI中显示内部版本号。
我必须使用AndroidManifest.xml吗?
当前回答
Log.e("TAG","VERSION.RELEASE {" + Build.VERSION.RELEASE + "}");
Log.e("TAG","\nVERSION.INCREMENTAL {" + Build.VERSION.INCREMENTAL + "}");
Log.e("TAG","\nVERSION.SDK {" + Build.VERSION.SDK + "}");
Log.e("TAG","\nBOARD {" + Build.BOARD + "}");
Log.e("TAG","\nBRAND {" + Build.BRAND + "}");
Log.e("TAG","\nDEVICE {" + Build.DEVICE + "}");
Log.e("TAG","\nFINGERPRINT {" + Build.FINGERPRINT + "}");
Log.e("TAG","\nHOST {" + Build.HOST + "}");
Log.e("TAG","\nID {" + Build.ID + "}");
幸亏https://stackoverflow.com/a/40421709/2828651
其他回答
这是一个干净的解决方案,基于scottyab(由哈维编辑)的解决方案。它显示了如果方法没有提供上下文,如何首先获取上下文。此外,它使用多行而不是每行调用多个方法。这使您在调试应用程序时更容易。
Context context = getApplicationContext(); // or activity.getApplicationContext()
PackageManager packageManager = context.getPackageManager();
String packageName = context.getPackageName();
String myVersionName = "not available"; // initialize String
try {
myVersionName = packageManager.getPackageInfo(packageName, 0).versionName;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
现在您在StringmyVersionName中收到了版本名,您可以将其设置为TextView或任何您喜欢的内容。。
// Set the version name to a TextView
TextView tvVersionName = (TextView) findViewById(R.id.tv_versionName);
tvVersionName.setText(myVersionName);
如果您使用的是PhoneGap,请创建自定义PhoneGa插件:
在应用程序包中创建新类:
package com.Demo; //replace with your package name
import org.json.JSONArray;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import com.phonegap.api.Plugin;
import com.phonegap.api.PluginResult;
import com.phonegap.api.PluginResult.Status;
public class PackageManagerPlugin extends Plugin {
public final String ACTION_GET_VERSION_NAME = "GetVersionName";
@Override
public PluginResult execute(String action, JSONArray args, String callbackId) {
PluginResult result = new PluginResult(Status.INVALID_ACTION);
PackageManager packageManager = this.ctx.getPackageManager();
if(action.equals(ACTION_GET_VERSION_NAME)) {
try {
PackageInfo packageInfo = packageManager.getPackageInfo(
this.ctx.getPackageName(), 0);
result = new PluginResult(Status.OK, packageInfo.versionName);
}
catch (NameNotFoundException nnfe) {
result = new PluginResult(Status.ERROR, nnfe.getMessage());
}
}
return result;
}
}
在plugins.xml中,添加以下行:
<plugin name="PackageManagerPlugin" value="com.Demo.PackageManagerPlugin" />
在deviceready事件中,添加以下代码:
var PackageManagerPlugin = function() {
};
PackageManagerPlugin.prototype.getVersionName = function(successCallback, failureCallback) {
return PhoneGap.exec(successCallback, failureCallback, 'PackageManagerPlugin', 'GetVersionName', []);
};
PhoneGap.addConstructor(function() {
PhoneGap.addPlugin('packageManager', new PackageManagerPlugin());
});
然后,您可以通过执行以下操作获取versionName属性:
window.plugins.packageManager.getVersionName(
function(versionName) {
//do something with versionName
},
function(errorMessage) {
//do something with errorMessage
}
);
源自这里和这里。
始终使用try-catch块:
String versionName = "Version not found";
try {
versionName = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName;
Log.i(TAG, "Version Name: " + versionName);
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
Log.e(TAG, "Exception Version Name: " + e.getLocalizedMessage());
}
try {
PackageInfo packageInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
String versionName = packageInfo.versionName;
int versionCode = packageInfo.versionCode;
//binding.tvVersionCode.setText("v" + packageInfo.versionName);
}
catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
Kotlin单层
val versionCode = BuildConfig.VERSION_CODE
val versionName = BuildConfig.VERSION_NAME
Java一行程序
String versionCode = String.valueOf(BuildConfig.VERSION_CODE);
String versionName = String.valueOf(BuildConfig.VERSION_NAME);
确保将BuildConfig导入到类中。