给Android应用程序的根访问权限
问题描述:
我正在构建一个应用程序,该应用程序必须具有超级用户权限才能在/ data目录中写入修改数据库文件的内容。我写一些代码,获得root访问权限,但它给我的日志不能“获得root权限或用户拒绝”给Android应用程序的根访问权限
我的代码是
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(canRunRootCommands())
Toast.makeText(this, "Can Run Root Commands", Toast.LENGTH_LONG).show();
}
@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;
}
public static boolean canRunRootCommands()
{
boolean retval = false;
Process suProcess;
try
{
suProcess = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(suProcess.getOutputStream());
DataInputStream osRes = new DataInputStream(suProcess.getInputStream());
if (null != os && null != osRes)
{
// Getting the id of the current user to check if this is root
os.writeBytes("id\n");
os.flush();
String currUid = osRes.readLine();
boolean exitSu = false;
if (null == currUid)
{
retval = false;
exitSu = false;
Log.d("ROOT", "Can't get root access or denied by user");
}
else if (true == currUid.contains("uid=0"))
{
retval = true;
exitSu = true;
Log.d("ROOT", "Root access granted");
}
else
{
retval = false;
exitSu = true;
Log.d("ROOT", "Root access rejected: " + currUid);
}
if (exitSu)
{
os.writeBytes("exit\n");
os.flush();
}
}
}
catch (Exception e)
{
// Can't get root !
// Probably broken pipe exception on trying to write to output stream (os) after su failed, meaning that the device is not rooted
retval = false;
Log.d("ROOT", "Root access rejected [" + e.getClass().getName() + "] : " + e.getMessage());
}
return retval;
}
如何获得root权限的,这样我可以更新数据库文件?
答
使用RootTools
库,它使根更容易,您不需要使用Process
并使用大量的代码。
答
在正常情况下,除非设备已生根,否则根不是您可以实现的。您不能将您的应用程序的FORCE根目录放在非根目录条件之外。请研究根意味着什么。
大多数Android设备的根访问权限受到限制。你必须得到这一点,通常排除设备的保修...... – ppeterka 2013-02-26 10:25:28
意味着我不能根本访问大多数的Android设备?通过任何方法? – 2013-02-26 10:33:36
没有。根植的访问权限仅限于决定根植他们电话(少数)的用户,并且明确允许您的应用程序根访问。 – Budius 2013-02-26 10:50:16