You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

41 lines
1.2 KiB
Java

package com.xypower.mpremote.utils;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import com.xypower.mpremote.R;
public class AppUtils {
/**
* 获取应用版本名称
* @param context 上下文
* @return 版本名称,如"1.0.0"
*/
public static String getVersionName(Context context) {
try {
PackageInfo packageInfo = context.getPackageManager()
.getPackageInfo(context.getPackageName(), 0);
return packageInfo.versionName;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
return null;
}
}
/**
* 获取带版本名称的应用名称
* @param context 上下文
* @return 如"我的App v1.0.0"
*/
public static String getAppNameWithVersion(Context context) {
try {
String appName = context.getString(R.string.app_name); // 从strings.xml获取
String versionName = getVersionName(context);
return String.format("%s v%s", appName, versionName);
} catch (Exception e) {
return context.getString(R.string.app_name);
}
}
}