public classApkController {/*** 描述: 安装*/
public static booleaninstall(String apkPath,Context context){//先判断手机是否有root权限
if(hasRootPerssion()){//有root权限,利用静默安装实现
returnclientInstall(apkPath);
}else{//没有root权限,利用意图进行安装
File file = newFile(apkPath);if(!file.exists())return false;
Intent intent= newIntent();
intent.setAction("android.intent.action.VIEW");
intent.addCategory("android.intent.category.DEFAULT");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setDataAndType(Uri.fromFile(file),"application/vnd.android.package-archive");
context.startActivity(intent);return true;
}
}/*** 描述: 卸载*/
public static booleanuninstall(String packageName,Context context){if(hasRootPerssion()){//有root权限,利用静默卸载实现
returnclientUninstall(packageName);
}else{
Uri packageURI= Uri.parse("package:" +packageName);
Intent uninstallIntent= newIntent(Intent.ACTION_DELETE,packageURI);
uninstallIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(uninstallIntent);return true;
}
}/*** 判断手机是否有root权限*/
public static booleanhasRootPerssion(){
PrintWriter PrintWriter= null;
Process process= null;try{
process= Runtime.getRuntime().exec("su");
PrintWriter= newPrintWriter(process.getOutputStream());
PrintWriter.flush();
PrintWriter.close();int value =process.waitFor();returnreturnResult(value);
}catch(Exception e) {
e.printStackTrace();
}finally{if(process!=null){
process.destroy();
}
}return false;
}/*** 静默安装*/
public static booleanclientInstall(String apkPath){
PrintWriter PrintWriter= null;
Process process= null;try{
process= Runtime.getRuntime().exec("su");
PrintWriter= newPrintWriter(process.getOutputStream());
PrintWriter.println("chmod 777 "+apkPath);
PrintWriter.println("export LD_LIBRARY_PATH=/vendor/lib:/system/lib");
PrintWriter.println("pm install -r "+apkPath);//PrintWriter.println("exit");
PrintWriter.flush();
PrintWriter.close();int value =process.waitFor();returnreturnResult(value);
}catch(Exception e) {
e.printStackTrace();
}finally{if(process!=null){
process.destroy();
}
}return false;
}/*** 静默卸载*/
public static booleanclientUninstall(String packageName){
PrintWriter PrintWriter= null;
Process process= null;try{
process= Runtime.getRuntime().exec("su");
PrintWriter= newPrintWriter(process.getOutputStream());
PrintWriter.println("LD_LIBRARY_PATH=/vendor/lib:/system/lib ");
PrintWriter.println("pm uninstall "+packageName);
PrintWriter.flush();
PrintWriter.close();int value =process.waitFor();returnreturnResult(value);
}catch(Exception e) {
e.printStackTrace();
}finally{if(process!=null){
process.destroy();
}
}return false;
}/*** 启动app
* com.exmaple.client/.MainActivity
* com.exmaple.client/com.exmaple.client.MainActivity*/
public static booleanstartApp(String packageName,String activityName){boolean isSuccess = false;
String cmd= "am start -n " + packageName + "/" + activityName + " \n";
Process process= null;try{
process=Runtime.getRuntime().exec(cmd);int value =process.waitFor();returnreturnResult(value);
}catch(Exception e) {
e.printStackTrace();
}finally{if(process!=null){
process.destroy();
}
}returnisSuccess;
}/*** 将文件复制到system/app 目录
*@paramapkPath 特别注意格式:该路径不能是:/storage/emulated/0/app/QDemoTest4.apk 需要是:/sdcard/app/QDemoTest4.apk
*@return
*/
public static booleancopy2SystemApp(String apkPath){
PrintWriter PrintWriter= null;
Process process= null;
String appName= "chetou.apk",cmd;try{
process= Runtime.getRuntime().exec("su");
PrintWriter= newPrintWriter(process.getOutputStream());
cmd= "mount -o remount,rw -t yaffs2 /dev/block/mtdblock3 /system";
Log.e("copy2SystemApp", cmd);
PrintWriter.println(cmd);
cmd= "cat "+apkPath+" > /system/app/"+appName;
Log.e("copy2SystemApp", cmd);
PrintWriter.println(cmd);
cmd= "chmod 777 /system/app/"+appName +" -R";
Log.e("copy2SystemApp", cmd);
PrintWriter.println(cmd);
cmd= "mount -o remount,ro -t yaffs2 /dev/block/mtdblock3 /system";
Log.e("copy2SystemApp", cmd);
PrintWriter.println(cmd);
PrintWriter.println("reboot"); //重启
PrintWriter.println("exit");
PrintWriter.flush();
PrintWriter.close();int value =process.waitFor();returnreturnResult(value);
}catch(Exception e) {
e.printStackTrace();
}finally{if(process!=null){
process.destroy();
}
}return false;
}private static boolean returnResult(intvalue){//代表成功
if (value == 0) {return true;
}else if (value == 1) { //失败
return false;
}else { //未知情况
return false;
}
}
}