安卓开发一般都需要进行apk的更新管理,常用操作老司机已为你封装完毕,经常有小伙伴问怎么进行管理,那我告诉你,你可以用这份工具进行管理,具体可以查看源码,现在为你开车,Demo传送门。
站点
获取App应用版本信息 → AppApplicationMgr
getAppName : 获取本地apk的名称
getVersionName : 获取本地Apk版本名称
getVersionCode : 获取本地Apk版本号
getMetaData : 根据key获取xml中Meta的值
getAppIcon : 获取应用图标
getAppFirstInstallTime : 获取应用第一次安装日期
getAppLastUpdateTime : 获取应用更新日期
getAppSize : 获取应用大小
getAppApk : 获取应用apk文件
getAppInstaller : 获取应用的安装市场
getAppSign : 获取应用签名
getAppTargetSdkVersion : 获取应用兼容sdk
getAppUid : 获取应用uid
getNumCores : 获取Cpu内核数
getRootPermission : 获得root权限
getAppPermissions : 获取应用的所有权限
hasPermission : 是否有权限
isInstalled : 应用是否安装
installApk : 安装应用
uninstallApk : 卸载应用
isSystemApp : 是否是系统应用
isServiceRunning : 服务是否在运行
stopRunningService : 停止服务
killProcesses : 结束进程
runScript : 运行脚本
runApp : 启动应用
getPackageName : 获得包名
getApplicationMetaData : 获取application层级的metadata
具体路线
@SuppressWarnings("rawtypes")
public class AppApplicationMgr {
/**
* 获取本地apk的名称
* @param context 上下文
* @return String
*/
public static String getAppName(Context context) {
try {
PackageManager e = context.getPackageManager();
PackageInfo packageInfo = e.getPackageInfo(context.getPackageName(), 0);
int labelRes = packageInfo.applicationInfo.labelRes;
return context.getResources().getString(labelRes);
} catch (NameNotFoundException var4) {
var4.printStackTrace();
return "unKnown";
}
}
/**
* 获取本地Apk版本名称
* @param context 上下文
* @return String
*/
public static String getVersionName(Context context) {
String verName = "";
try {
verName = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName;
} catch (NameNotFoundException e) {
AppLogMessageMgr.e("AppApplicationMgr-->>getVerName()", e.getMessage() + "获取本地Apk版本名称失败!");
e.printStackTrace();
}
return verName;
}
/**
* 获取本地Apk版本号
* @param context 上下文
* @return int
*/
public static int getVersionCode(Context context) {
int verCode = -1;
try {
verCode = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionCode;
} catch (NameNotFoundException e) {
AppLogMessageMgr.e("AppApplicationMgr-->>getVerCode()", e.getMessage() + "获取本地Apk版本号失败!");
e.printStackTrace();
}
return verCode;
}
/**
* 根据key获取xml中Meta的值
* @param context 上下文
* @param key
* @return
*/
public static String getMetaData(Context context, String key) {
String value = "";
try {
ApplicationInfo e = context.getPackageManager().getApplicationInfo(context.getPackageName(), 128);
if(null != e) {
Bundle metaData = e.metaData;
if(null != metaData) {
value = metaData.getString(key);
if(null == value || value.length() == 0) {
value = "";
}
}
}
} catch (NameNotFoundException var5) {
var5.printStackTrace();
}
return value;
}
/**
* 获取应用图标
* @param context
* @param packageName
* @return
*/
public static Drawable getAppIcon(Context context, String packageName) {
PackageManager pm = context.getPackageManager();
Drawable appIcon = null;
try {
ApplicationInfo applicationInfo = pm.getApplicationInfo(packageName, 0);
appIcon = applicationInfo.loadIcon(pm);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return appIcon;
}
/**
* 获取应用第一次安装日期
* @param context
* @param packageName
* @return
*/
public static long getAppFirstInstallTime(Context context, String packageName) {
long lastUpdateTime = 0;
try {
PackageInfo packageInfo = context.getPackageManager().getPackageInfo(packageName, 0);
lastUpdateTime = packageInfo.firstInstallTime;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return lastUpdateTime;
}
/**
* 获取应用更新日期
* @param context
* @param packageName
* @return
*/
public static long getAppLastUpdateTime(Context context, String packageName) {
long lastUpdateTime = 0;
try {
PackageInfo packageInfo = context.getPackageManager().getPackageInfo(packageName, 0);
lastUpdateTime = packageInfo.lastUpdateTime;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return lastUpdateTime;
}
/**
* 获取应用大小
* @param context
* @param packageName
* @return
*/
public static long getAppSize(Context context, String packageName) {
long appSize = 0;
try {
ApplicationInfo applicationInfo = context.getPackageManager().getApplicationInfo(packageName, 0);
appSize = new File(applicationInfo.sourceDir).length();
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return appSize;
}
/**
* 获取应用apk文件
* @param context
* @param packageName
* @return
*/
public static String getAppApk(Context context, String packageName) {
String sourceDir = null;
try {
ApplicationInfo applicationInfo = context.getPackageManager().getApplicationInfo(packageName, 0);
sourceDir = applicationInfo.sourceDir;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return sourceDir;
}
/**
* 获取应用的安装市场
* @param context
* @param packageName
* @return
*/
public static String getAppInstaller(Context context, String packageName) {
return context.getPackageManager().getInstallerPackageName(packageName);
}
/**
* 获取应用签名
* @param context
* @param packageName
* @return
*/
public static String getAppSign(Context context, String packageName) {
try {
PackageInfo pis = context.getPackageManager().getPackageInfo(packageName, PackageManager.GET_SIGNATURES);
return hexdigest(pis.signatures[0].toByteArray());
} catch (PackageManager.NameNotFoundException e) {
throw new RuntimeException(AppApplicationMgr.class.getName() + "the " + packageName + "'s application not found");
}
}
public static String hexdigest(byte[] paramArrayOfByte) {
final char[] hexDigits = {48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98, 99, 100, 101, 102};
try {
MessageDigest localMessageDigest = MessageDigest.getInstance("MD5");
localMessageDigest.update(paramArrayOfByte);
byte[] arrayOfByte = localMessageDigest.digest();
char[] arrayOfChar = new char[32];
for (int i = 0, j = 0; ; i++, j++) {
if (i >= 16) {
return new String(arrayOfChar);
}
int k = arrayOfByte[i];
arrayOfChar[j] = hexDigits[(0xF & k >>> 4)];
arrayOfChar[++j] = hexDigits[(k & 0xF)];
}
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
/**
* 获取应用兼容sdk
* @param context
* @param packageName
* @return
*/
public static int getAppTargetSdkVersion(Context context, String packageName) {
try {
PackageInfo packageInfo = context.getPackageManager().getPackageInfo(packageName, 0);
ApplicationInfo applicationInfo = packageInfo.applicationInfo;
return applicationInfo.targetSdkVersion;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
return 0;
}
}
/**
* 获取应用uid
* @param context
* @param packageName
* @return
*/
public static int getAppUid(Context context, String packageName) {
try {
PackageInfo packageInfo = context.getPackageManager().getPackageInfo(packageName,