我需要弄清楚如何获取或制作Android应用程序的版本号。我需要在UI中显示内部版本号。
我必须使用AndroidManifest.xml吗?
我需要弄清楚如何获取或制作Android应用程序的版本号。我需要在UI中显示内部版本号。
我必须使用AndroidManifest.xml吗?
当前回答
我已经通过使用Preference类解决了这个问题。
package com.example.android;
import android.content.Context;
import android.preference.Preference;
import android.util.AttributeSet;
public class VersionPreference extends Preference {
public VersionPreference(Context context, AttributeSet attrs) {
super(context, attrs);
String versionName;
final PackageManager packageManager = context.getPackageManager();
if (packageManager != null) {
try {
PackageInfo packageInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
versionName = packageInfo.versionName;
} catch (PackageManager.NameNotFoundException e) {
versionName = null;
}
setSummary(versionName);
}
}
}
其他回答
如果您使用的是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
}
);
源自这里和这里。
Use:
try {
PackageInfo pInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
String version = pInfo.versionName;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
您可以使用以下命令获取版本代码
int verCode = pInfo.versionCode;
由于我只需要获取版本代码并检查应用程序是否更新,如果是,我必须启动play商店以获取更新的应用程序。我是这样做的。
public class CheckForUpdate {
public static final String ACTION_APP_VERSION_CHECK = "app-version-check";
public static void launchPlayStoreApp(Context context)
{
// getPackageName() from Context or Activity object
final String appPackageName = context.getPackageName();
try {
context.startActivity(new Intent(Intent.ACTION_VIEW,
Uri.parse("market://details?id=" + appPackageName)));
}
catch (android.content.ActivityNotFoundException anfe) {
context.startActivity(new Intent(Intent.ACTION_VIEW,
Uri.parse("https://play.google.com/store/apps/details?id=" +
appPackageName)));
}
}
public static int getRemoteVersionNumber(Context context)
{
int versionCode = 0;
try {
PackageInfo pInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
String version = pInfo.versionName;
versionCode = pInfo.versionCode;
}
catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return versionCode;
}
}
然后,我通过创建一个util类,使用共享首选项保存了版本代码。
public class PreferenceUtils {
// This is for version code
private final String APP_VERSION_CODE = "APP_VERSION_CODE";
private SharedPreferences sharedPreferencesAppVersionCode;
private SharedPreferences.Editor editorAppVersionCode;
private static Context mContext;
public PreferenceUtils(Context context)
{
this.mContext = context;
// This is for the app versioncode
sharedPreferencesAppVersionCode = mContext.getSharedPreferences(APP_VERSION_CODE,MODE_PRIVATE);
editorAppVersionCode = sharedPreferencesAppVersionCode.edit();
}
public void createAppVersionCode(int versionCode) {
editorAppVersionCode.putInt(APP_VERSION_CODE, versionCode);
editorAppVersionCode.apply();
}
public int getAppVersionCode()
{
return sharedPreferencesAppVersionCode.getInt(APP_VERSION_CODE,0); // As the default version code is 0
}
}
不,您不需要对AndroidManifest.xml执行任何操作
基本上,应用程序的版本名称和版本代码位于应用程序级Gradle文件中的defaultConfig标记下:
defaultConfig {
versionCode 1
versionName "1.0"
}
注意:当您希望将应用程序上载到play商店时,它可以提供任何名称作为版本名称,但如果此应用程序已在play商店中,则版本代码必须与当前版本代码不同。
只需使用以下代码片段即可从应用程序中的任何位置获取版本代码和版本名称:
try {
PackageInfo pInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
String version = pInfo.versionName;
int verCode = pInfo.versionCode;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
对构建系统有用:有一个用APK文件生成的文件,名为output.json,其中包含每个生成的APK文件的一系列信息,包括versionName和versionCode。
例如
[
{
"apkInfo": {
"baseName": "x86-release",
"enabled": true,
"filterName": "x86",
"fullName": "86Release",
"outputFile": "x86-release-1.0.apk",
"splits": [
{
"filterType": "ABI",
"value": "x86"
}
],
"type": "FULL_SPLIT",
"versionCode": 42,
"versionName": "1.0"
},
"outputType": {
"type": "APK"
},
"path": "app-x86-release-1.0.apk",
"properties": {}
}
]