客户对产品的要求进一步升级,对产品做Andirod的root权限检测。说实话,现在很多手机基本上root权限已经回收了,特别是华为的,今天准备在/data/目录下touch一个文件都不行:
但没办法客户有需求,那么Andirod的root权限检测代码:
public class MainActivity extends Activity {@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
doSU();
}
@Override
protected void onResume() {
super.onResume();
doSU();
}
private void doSU() {
try {
Process process = Runtime.getRuntime().exec("su");// (这里执行是系统已经开放了root权限,而不是说通过执行这句来获得root权限)
DataOutputStream os = new DataOutputStream(process.getOutputStream());
// os.writeBytes("ifconfig eth0 192.168.18.122\n");
os.writeBytes("exit\n");
os.flush();
/*
* //如果已经root,但是用户选择拒绝授权,e.getMessage() = write failed: EPIPE (Broken pipe)
//如果没有root,,e.getMessage()= Error running exec(). Command: [su] Working Directory: null Environment: null
*/
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
上面的代码放到jar工程里面,打包前运行一下,看可否结果怎么样,基本可以做到root权限的检测。
只不过是要不要给客户提供一个友好性提示,得前段要重新设置一下。。