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);
}
}
}