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});
/*** Automatically generated file. DO NOT MODIFY*/package com.XXX.Project;
public final class BuildConfig {public static final boolean DEBUG = Boolean.parseBoolean("true");public static final String APPLICATION_ID = "com.XXX.Project";public static final String BUILD_TYPE = "debug";public static final String FLAVOR = "";public static final int VERSION_CODE = 1;public static final String VERSION_NAME = "1.0.0";}
通过Python脚本或其他工具拆分您需要的信息。这是一个示例:
import subprocess# Find your BuildConfig.java_BuildConfig = subprocess.check_output('find {WORKSPACE} -name BuildConfig.java', shell=True).rstrip()
# Get the version name_Android_version = subprocess.check_output('grep -n "VERSION_NAME" ' + _BuildConfig, shell=True).split('"')[1]print('Android version: ’ + _Android_version)
public string getVersionName(){return Application.Context.ApplicationContext.PackageManager.GetPackageInfo(Application.Context.ApplicationContext.PackageName, 0).VersionName;}
版本代码:
public string getVersionCode(){return Application.Context.ApplicationContext.PackageManager.GetPackageInfo(Application.Context.ApplicationContext.PackageName, 0).VersionCode;}
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
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 objectfinal 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 codeprivate 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 versioncodesharedPreferencesAppVersionCode = 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}}
fun Activity.getVersionCode(): Int = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {packageManager.getPackageInfo(packageName, 0).longVersionCode.toInt()} else {packageManager.getPackageInfo(packageName, 0).versionCode}