我需要弄清楚如何获取或制作Android应用程序的版本号。我需要在UI中显示内部版本号。
我必须使用AndroidManifest.xml吗?
我需要弄清楚如何获取或制作Android应用程序的版本号。我需要在UI中显示内部版本号。
我必须使用AndroidManifest.xml吗?
当前回答
内部片段用法示例。
import android.content.pm.PackageManager;
.......
private String VersionName;
private String VersionCode;
.......
Context context = getActivity().getApplicationContext();
/* Getting application version name and code */
try
{
VersionName = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName;
/* I find it useful to convert vervion code into String,
so it's ready for TextViev/server side checks
*/
VersionCode = Integer.toString(context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionCode);
}
catch (PackageManager.NameNotFoundException e)
{
e.printStackTrace();
}
// Do something useful with that
其他回答
这段代码在上面提到过,但在这里,它再次全部包含在内。您需要try/catch块,因为它可能引发“NameNotFoundException”。
try {
String appVersion = getPackageManager().getPackageInfo(getPackageName(), 0).versionName;
}
catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
我希望这能为将来的人简化事情
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
对于Xamarin用户,使用此代码获取版本名称和代码
版本名称:公共字符串getVersionName(){return Application.Context.ApplicationContext.BackageManager.GetPackageInfo(Application.Context.ApplicationContext.PackageName,0).VersionName;}版本代码:公共字符串getVersionCode(){return Application.Context.ApplicationContext.BackageManager.GetPackageInfo(Application.Context.ApplicationContext.PackageName,0).VersionCode;}
private String GetAppVersion() {
try {
PackageInfo _info = mContext.getPackageManager().getPackageInfo(mContext.getPackageName(), 0);
return _info.versionName;
}
catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
return "";
}
}
private int GetVersionCode() {
try {
PackageInfo _info = mContext.getPackageManager().getPackageInfo(mContext.getPackageName(), 0);
return _info.versionCode;
}
catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
return -1;
}
}
如果您使用的是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
}
);
源自这里和这里。