手机总是出现android系统正在请求root权限授权管理,请先在授权管理中启用Root

android在apk中获取root权限,并执行命令
&& 在apk中,有时候需要root权限,例如通过apk更新库等system的文件等,避免升级固件,或者在apk中需要直接访问某些设备等。下面是在apk中获取root权限的方法,前提是设备已经root过了。
&& 关键点在于下面这句,通过执行su产生一个具有root权限的进程:
Process p = Runtime.getRuntime().exec(&su&);
然后,在向这个进程的写入要执行的命令,即可达到以root权限执行命令:
= new DataOutputStream(p.getOutputStream());
dos.writeBytes(cmd + &\n&);
dos.flush();
或者用下面的方式:
Runtime.getRuntime().exec(new String[]{&/system/bin/su&,&-c&, cmd});
经过测试,以root权限执行命令,只在真机上测试成功,在模拟器上没有成功过。
第一次运行时,会出现请求root权限的界面,选中记住,并允许:
测试程序界面,如果已经root,界面中可以显示出/system分区对应的设备节点:
主要文件:RootCmd.java
package org.ckl.&
import java.io.DataInputS&
import java.io.DataOutputS&
import java.io.IOE&
import android.util.L&
public final class RootCmd {&
&&& private static final String TAG = &RootCmd&;&
&&& private static boolean mHaveRoot =&
&&& // 判断机器是否已经root,即是否获取root权限&
&&& public static boolean haveRoot() {&
&&&&&&& if (!mHaveRoot) {&
&&&&&&&&&&& int ret = execRootCmdSilent(&echo test&); // 通过执行测试命令来检测&
&&&&&&&&&&& if (ret != -1) {&
&&&&&&&&&&&&&&& Log.i(TAG, &have root!&);&
&&&&&&&&&&&&&&& mHaveRoot =&
&&&&&&&&&&& } else {&
&&&&&&&&&&&&&&& Log.i(TAG, &not root!&);&
&&&&&&&&&&& }&
&&&&&&& } else {&
&&&&&&&&&&& Log.i(TAG, &mHaveRoot = true, have root!&);&
&&&&&&& }&
&&&&&&& return mHaveR&
&&& // 执行命令并且输出结果&
&&& public static String execRootCmd(String cmd) {&
&&&&&&& String result = &&;&
&&&&&&& DataOutputStream dos =&
&&&&&&& DataInputStream dis =&
&&&&&&& try {&
&&&&&&&&&&& Process p = Runtime.getRuntime().exec(&su&);// 经过Root处理的android系统即有su命令&
&&&&&&&&&&& dos = new DataOutputStream(p.getOutputStream());&
&&&&&&&&&&& dis = new DataInputStream(p.getInputStream());&
&&&&&&&&&&& Log.i(TAG, cmd);&
&&&&&&&&&&& dos.writeBytes(cmd + &\n&);&
&&&&&&&&&&& dos.flush();&
&&&&&&&&&&& dos.writeBytes(&exit\n&);&
&&&&&&&&&&& dos.flush();&
&&&&&&&&&&& String line =&
&&&&&&&&&&& while ((line = dis.readLine()) != null) {&
&&&&&&&&&&&&&&& Log.d(&result&, line);&
&&&&&&&&&&&&&&& result +=&
&&&&&&&&&&& }&
&&&&&&&&&&& p.waitFor();&
&&&&&&& } catch (Exception e) {&
&&&&&&&&&&& e.printStackTrace();&
&&&&&&& } finally {&
&&&&&&&&&&& if (dos != null) {&
&&&&&&&&&&&&&&& try {&
&&&&&&&&&&&&&&&&&&& dos.close();&
&&&&&&&&&&&&&&& } catch (IOException e) {&
&&&&&&&&&&&&&&&&&&& e.printStackTrace();&
&&&&&&&&&&&&&&& }&
&&&&&&&&&&& }&
&&&&&&&&&&& if (dis != null) {&
&&&&&&&&&&&&&&& try {&
&&&&&&&&&&&&&&&&&&& dis.close();&
&&&&&&&&&&&&&&& } catch (IOException e) {&
&&&&&&&&&&&&&&&&&&& e.printStackTrace();&
&&&&&&&&&&&&&&& }&
&&&&&&&&&&& }&
&&&&&&& }&
&&& // 执行命令但不关注结果输出&
&&& public static int execRootCmdSilent(String cmd) {&
&&&&&&& int result = -1;&
&&&&&&& DataOutputStream dos =&
&&&&&&& try {&
&&&&&&&&&&& Process p = Runtime.getRuntime().exec(&su&);&
&&&&&&&&&&& dos = new DataOutputStream(p.getOutputStream());&
&&&&&&&&&&&&&
&&&&&&&&&&& Log.i(TAG, cmd);&
&&&&&&&&&&& dos.writeBytes(cmd + &\n&);&
&&&&&&&&&&& dos.flush();&
&&&&&&&&&&& dos.writeBytes(&exit\n&);&
&&&&&&&&&&& dos.flush();&
&&&&&&&&&&& p.waitFor();&
&&&&&&&&&&& result = p.exitValue();&
&&&&&&& } catch (Exception e) {&
&&&&&&&&&&& e.printStackTrace();&
&&&&&&& } finally {&
&&&&&&&&&&& if (dos != null) {&
&&&&&&&&&&&&&&& try {&
&&&&&&&&&&&&&&&&&&& dos.close();&
&&&&&&&&&&&&&&& } catch (IOException e) {&
&&&&&&&&&&&&&&&&&&& e.printStackTrace();&
&&&&&&&&&&&&&&& }&
&&&&&&&&&&& }&
&&&&&&& }&
相关文件:SystemPartition.java,获取/system分区设备节点,并支持重新mount /system为可读写:
package org.ckl.&
import java.io.DataInputS&
import java.io.F&
import java.io.FileInputS&
import java.io.IOE&
import android.util.L&
public class SystemPartition {&
&&& private static final String TAG = &SystemMount&;&
&&& private static String TMP_PATH = &/sdcard/mount.txt&;&
&&& private static String mMountPiont =&
&&& private static boolean mWriteable =&
&&& private SystemPartition() {&
&&&&&&& Log.i(TAG, &new SystemMount()&);&
&&& private static class SystemPartitionHolder {&
&&&&&&& private static SystemPartition instance = new SystemPartition();&
&&& public SystemPartition getInstance() {&
&&&&&&& return SystemPartitionHolder.&
&&& public static String getSystemMountPiont() {&
&&&&&&& DataInputStream dis =&
&&&&&&& if (mMountPiont == null) {&&
&&&&&&&&&&& try {&
&&&&&&&&&&&&&&& RootCmd.execRootCmd(&mount & & + TMP_PATH);&
//&&&&&&&&&&&&& Runtime.getRuntime().exec(&mount & & + TMP_PATH);&
&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&& dis = new DataInputStream(new FileInputStream(TMP_PATH));&
&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&& String line =&
&&&&&&&&&&&&&&& int index = -1;&
&&&&&&&&&&&&&&& while ( (line = dis.readLine()) != null ) {&
&&&&&&&&&&&&&&&&&&& index = line.indexOf(& /system &);&
&&&&&&&&&&&&&&&&&&& if (index & 0) {&
&&&&&&&&&&&&&&&&&&&&&&& mMountPiont = line.substring(0, index);&
&&&&&&&&&&&&&&&&&&&&&&& if (line.indexOf(& rw&) & 0) {&
&&&&&&&&&&&&&&&&&&&&&&&&&&& mWriteable =&
&&&&&&&&&&&&&&&&&&&&&&&&&&& Log.i(TAG, &/system is writeable !&);&
&&&&&&&&&&&&&&&&&&&&&&& } else {&
&&&&&&&&&&&&&&&&&&&&&&&&&&& mWriteable =&
&&&&&&&&&&&&&&&&&&&&&&&&&&& Log.i(TAG, &/system is readonly !&);&
&&&&&&&&&&&&&&&&&&&&&&& }&
&&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&& }&
&&&&&&&&&&&&&&& }&
&&&&&&&&&&& } catch (Exception e) {&
&&&&&&&&&&&&&&& e.printStackTrace();&
&&&&&&&&&&& } finally {&
&&&&&&&&&&&&&&& if (dis != null) {&
&&&&&&&&&&&&&&&&&&& try {&
&&&&&&&&&&&&&&&&&&&&&&& dis.close();&
&&&&&&&&&&&&&&&&&&& } catch (IOException e1) {&
&&&&&&&&&&&&&&&&&&&&&&& e1.printStackTrace();&
&&&&&&&&&&&&&&&&&&& }&
&&&&&&&&&&&&&&&&&&& dis =&
&&&&&&&&&&&&&&& }&
&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&& File f = new File(TMP_PATH);&
&&&&&&&&&&&&&&& if (f.exists()) {&
&&&&&&&&&&&&&&&&&&& f.delete();&
&&&&&&&&&&&&&&& }&
&&&&&&&&&&& }&
&&&&&&& }&
&&&&&&& if (mMountPiont != null) {&
&&&&&&&&&&& Log.i(TAG, &/system mount piont: & + mMountPiont);&
&&&&&&& } else {&
&&&&&&&&&&& Log.i(TAG, &get /system mount piont failed !!!&);&
&&&&&&& }&
&&&&&&& return mMountP&
&&& public static boolean isWriteable() {&
&&&&&&& mMountPiont =&
&&&&&&& getSystemMountPiont();&
&&&&&&& return mW&
&&& public static void remountSystem(boolean writeable) {&
&&&&&&& String cmd =&
&&&&&&& getSystemMountPiont();&
&&&&&&& if (mMountPiont != null && RootCmd.haveRoot()) {&
&&&&&&&&&&& if (writeable) {&
&&&&&&&&&&&&&&& cmd = &mount -o remount,rw & + mMountPiont + & /system&;&
&&&&&&&&&&& } else {&
&&&&&&&&&&&&&&& cmd = &mount -o remount,ro & + mMountPiont + & /system&;&
&&&&&&&&&&& }&
&&&&&&&&&&& RootCmd.execRootCmdSilent(cmd);&
&&&&&&&&&&&&&
&&&&&&&&&&& isWriteable();&
&&&&&&& }&
(window.slotbydup=window.slotbydup || []).push({
id: '2467140',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467141',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467142',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467143',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467148',
container: s,
size: '1000,90',
display: 'inlay-fix'您的举报已经提交成功,我们将尽快处理,谢谢!
那个东西再下载一个就行了,没有什么影响,只要你没有删除系统关键程序就行!!root刷机神马的,小白的话推荐用刷机精灵
在设置里有个恢复出厂设置,点击那里就可以了,不过记得得备份文件。
停止,是音软件运行部正常,建议重新启动,或者备份手机联系人,把手机恢复出厂设置,在重新下载软件吧 。希望你满意
大家还关注

我要回帖

更多关于 怎么授权root权限 的文章

 

随机推荐