调用android系统隐藏的api,所以需要导入jar包
第一步:
/**
* 判断手机是否有root权限
*/
public static boolean hasRootPerssion(){
PrintWriter PrintWriter = null;
Process process = null;
try {
process = Runtime.getRuntime().exec("su");
PrintWriter = new PrintWriter(process.getOutputStream());
PrintWriter.flush();
PrintWriter.close();
int value = process.waitFor();
return returnResult(value);
} catch (Exception e) {
e.printStackTrace();
}finally{
if(process!=null){
process.destroy();
}
}
return false;
}
如果有权限,直接安装就好
/**
* 系统的静默安装
*
* @param fileName
* @param packageName
*/
public void Install(String fileName, String packageName,
InstallCallBack callback, Context context)//
{
if (callback != null) {
CommenUtil.installBack = callback;
}
Uri uri = Uri.fromFile(new File(fileName));
int installFlags = 0;
PackageManager pm = context.getPackageManager();
try {
PackageInfo pi = pm.getPackageInfo(packageName,
PackageManager.GET_UNINSTALLED_PACKAGES);//
if (pi != null) {
installFlags |= PackageManager.INSTALL_REPLACE_EXISTING;
}
} catch (NameNotFoundException e) {
}
PackageInstallObserver observer = new PackageInstallObserver();
pm.installPackage(uri, observer, installFlags, packageName);
}
// 用于显示结果
class PackageInstallObserver extends IPackageInstallObserver.Stub {
public void packageInstalled(String packageName, int returnCode) {
Log.i("syso", "returnCode:" + returnCode);
if (returnCode == 1) {
Log.i("syso", "静默安装成功");
} else {
Log.i("syso", "静默安装失败");
if (installBack != null) {
installBack.SliceInstallFail();
}
}
}
};
回调函数里面自己实现相应的逻辑就好。
jar包下载地址:http://download.csdn.net/detail/zhuxiaoxuandd/9088485