Android检测设备是否有root权限
2013 年 7 月 3 日 Android检测设备是否有root权限无评论
安卓设备的root指第三方应用可以运行系统权限,即调用su来实现linux命令。单纯的eng版本的设备虽然可以通过adb修改系统但不具有root权限,因为其不包含superuser和su,第三方应用无法调用。执行如下代码获取设备是否已被root,没有root的设备会弹出"设备没有被root"的信息:
package com.example.testdroid;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(isRooted()){
Toast.makeText(this, "设备已被root", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "设备没有被root", Toast.LENGTH_LONG).show();
}
}
public DataInputStream Terminal(String command) throws Exception
{
Process process = Runtime.getRuntime().exec("su");
//执行到这,Superuser会跳出来,选择是否允许获取最高权限
OutputStream outstream = process.getOutputStream();
DataOutputStream DOPS = new DataOutputStream(outstream);
InputStream instream = process.getInputStream();
DataInputStream DIPS = new DataInputStream(instream);
String temp = command + "\n";
//加回车
DOPS.writeBytes(temp);
//执行
DOPS.flush();
//刷新,确保都发送到outputstream
DOPS.writeBytes("exit\n");
//退出
DOPS.flush();
process.waitFor();
return DIPS;
}
public boolean isRooted() {
//检测是否ROOT过
DataInputStream stream;
boolean flag=false;
try {
stream = Terminal("ls /data/");
//目录哪都行,不一定要需要ROOT权限的
if(stream.readLine()!=null)flag=true;
//根据是否有返回来判断是否有root权限
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return flag;
}
}
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(isRooted()){
Toast.makeText(this, "设备已被root", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "设备没有被root", Toast.LENGTH_LONG).show();
}
}
public DataInputStream Terminal(String command) throws Exception
{
Process process = Runtime.getRuntime().exec("su");
//执行到这,Superuser会跳出来,选择是否允许获取最高权限
OutputStream outstream = process.getOutputStream();
DataOutputStream DOPS = new DataOutputStream(outstream);
InputStream instream = process.getInputStream();
DataInputStream DIPS = new DataInputStream(instream);
String temp = command + "\n";
//加回车
DOPS.writeBytes(temp);
//执行
DOPS.flush();
//刷新,确保都发送到outputstream
DOPS.writeBytes("exit\n");
//退出
DOPS.flush();
process.waitFor();
return DIPS;
}
public boolean isRooted() {
//检测是否ROOT过
DataInputStream stream;
boolean flag=false;
try {
stream = Terminal("ls /data/");
//目录哪都行,不一定要需要ROOT权限的
if(stream.readLine()!=null)flag=true;
//根据是否有返回来判断是否有root权限
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return flag;
}
}
参考:http://blog.csdn.net/wop_niaoren19870227/article/details/7077786
发表评论