android安装root,android开发实现静默安装(root权限)
⽅式是将应⽤设置为内置的系统应⽤,注意事system/app⽬录下⾯,采⽤copy2SystemApp()⽅法就可以,注意chmod 777的权限,若是直接将apk拷贝到system/app⽬录,没有这个权限还是不能静默安装的。
直接贴出⼯具类:
public class ApkController {
/**
* 描述: 安装
*/
public static boolean install(String apkPath,Context context){
// 先判断⼿机是否有root权限
if(hasRootPerssion()){
// 有root权限,利⽤静默安装实现
return clientInstall(apkPath);
}else{
// 没有root权限,利⽤意图进⾏安装
File file = new File(apkPath);
if(!ists())
return false;
Intent intent = new Intent();
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 boolean uninstall(String packageName,Context context){
if(hasRootPerssion()){
// 有root权限,利⽤静默卸载实现
return clientUninstall(packageName);
}else{
Uri packageURI = Uri.parse("package:" + packageName);
Intent uninstallIntent = new Intent(Intent.ACTION_DELETE,packageURI); uninstallIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(uninstallIntent);
return true;
}
}
/**
* 判断⼿机是否有root权限
*/
public static boolean hasRootPerssion(){
PrintWriter PrintWriter = null;
Process process = null;
try {
process = Runtime().exec("su");
PrintWriter = new OutputStream()); PrintWriter.flush();
PrintWriter.close();
int value = process.waitFor();
return returnResult(value);
} catch (Exception e) {
e.printStackTrace();
}finally{
if(process!=null){
process.destroy();
}
}
return false;
}
/**
* 静默安装
*/
public static boolean clientInstall(String apkPath){
PrintWriter PrintWriter = null;
Process process = null;
eclipse开发手机apptry {
process = Runtime().exec("su");
PrintWriter = new OutputStream());
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();
return returnResult(value);
} catch (Exception e) {
e.printStackTrace();
}finally{
if(process!=null){
process.destroy();
}
}
return false;
}
/**
* 静默卸载
*/
public static boolean clientUninstall(String packageName){
PrintWriter PrintWriter = null;
Process process = null;
try {
process = Runtime().exec("su");
PrintWriter = new OutputStream());
PrintWriter.println("LD_LIBRARY_PATH=/vendor/lib:/system/lib "); PrintWriter.println("pm uninstall "+packageName);
PrintWriter.flush();
PrintWriter.close();
int value = process.waitFor();
return returnResult(value);
} catch (Exception e) {
e.printStackTrace();
}finally{
if(process!=null){
process.destroy();
}
}
return false;
}
/
**
* 启动app
* aple.client/.MainActivity
* aple.aple.client.MainActivity
*/
public static boolean startApp(String packageName,String activityName){ boolean isSuccess = false;
String cmd = "am start -n " + packageName + "/" + activityName + " \n"; Process process = null;
try {
process = Runtime().exec(cmd);
int value = process.waitFor();
return returnResult(value);
} catch (Exception e) {
e.printStackTrace();
} finally{
if(process!=null){
process.destroy();
}
}
return isSuccess;
}
/**
* 将⽂件复制到system/app ⽬录
* @param apkPath 特别注意格式:该路径不能是:/storage/emulated/0/app/QDemoTest4.apk 需要是:/sdcard/app/QDemoTest4.apk
* @return
*/
public static boolean copy2SystemApp(String apkPath){
PrintWriter PrintWriter = null;
Process process = null;
String appName = "chetou.apk",cmd;
try {
process = Runtime().exec("su");
PrintWriter = new OutputStream());
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();
return returnResult(value);
} catch (Exception e) {
e.printStackTrace();
}finally{
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论