本文主要是介绍检测手机是否具有root权限和静默安装,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
随时随地技术实战干货,获取项目源码、学习资料,请关注源代码社区公众号(ydmsq666)
执行静默安装时,需要向手机申请获取root权限,如果手机具有root权限,则可以完成安装。代码如下:
MainActivity:
package com.home.checkroot;import java.io.DataOutputStream;
import java.io.File;
import java.io.OutputStream;import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.util.Log;public class MainActivity extends Activity {private static final String TAG = "MainActivity";@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);// 笔者手机上的apk路径String apkName = Environment.getExternalStorageDirectory().toString()+ File.separator + "police.apk";Log.e(TAG, "root = " + haveRoot("pm install -r " + apkName));}/*** 检测是否具体root权限* * @param cmd* @return*/private boolean haveRoot(String cmd) {int i = execRootCmdSilent(cmd);if (i != -1) {return true;}return false;}/*** 执行静默安装* * @param paramString* @return*/private int execRootCmdSilent(String paramString) {int result = -1;try {Process localProcess = Runtime.getRuntime().exec("su");OutputStream os = localProcess.getOutputStream();DataOutputStream dos = new DataOutputStream(os);dos.writeBytes(paramString + "\n");dos.flush();dos.writeBytes("exit\n");dos.flush();localProcess.waitFor();result = localProcess.exitValue();} catch (Exception e) {e.printStackTrace();}return result;}
}
权限:
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" /><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
这篇关于检测手机是否具有root权限和静默安装的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!