android+ping+root,android开发实现静默安装(root权限)

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;

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值