安卓ROOT权限代码

这篇博客介绍了如何在Android设备上实现ROOT权限的代码操作,提供了一个工具类,包含执行手机指令的功能。需要注意的是,并非所有手机都能成功获取ROOT权限,因此在项目开发中应谨慎使用此类代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

android上root权限的代码,工具类还提供在手机上执行指令的方法。PS:并不是所有手机都能root成功,开发项目时尽量不要考虑root,这个代码看看就行。

[1].[代码] [Java]代码 跳至 [1]

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package com.linin.utils;
 
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
 
/**
  * root权限工具类
  *
  * @author linin
  *
  */
public class RootUtil {
 
     private static final String TAG = "linin.root" ;
     private static boolean mHaveRoot = false ;
 
     /**
      * 判断机器Android是否已经root,即是否获取root权限
      */
     public static boolean haveRoot() {
         if (!mHaveRoot) {
             int ret = execRootCmdSilent( "echo test" ); // 通过执行测试命令来检测
             if (ret != - 1 ) {
                 L.i(TAG, "have root!" );
                 mHaveRoot = true ;
             } else {
                 L.i(TAG, "not root!" );
             }
         } else {
             L.i(TAG, "mHaveRoot = true, have root!" );
         }
         return mHaveRoot;
     }
 
     /** 获取root权限 */
     public static boolean root() {
         try {
             Runtime.getRuntime().exec(
                     new String[] { "/system/bin/su" , "-c" ,
                             "chmod 777 /dev/graphics/fb0" });
         } catch (IOException e) {
             e.printStackTrace();
             L.i(TAG, "root fail!" );
             return false ;
         }
         L.i(TAG, "root success!" );
         return true ;
     }
 
     /**
      * 执行命令并且输出结果
      */
     public static String execRootCmd(String cmd) {
         String result = "" ;
         DataOutputStream dos = null ;
         DataInputStream dis = null ;
 
         try {
             Process p = Runtime.getRuntime().exec( "su" ); // 经过Root处理的android系统即有su命令
             dos = new DataOutputStream(p.getOutputStream());
             dis = new DataInputStream(p.getInputStream());
 
             L.i(TAG, cmd);
             dos.writeBytes(cmd + "\n" );
             dos.flush();
             dos.writeBytes( "exit\n" );
             dos.flush();
             String line = null ;
             while ((line = dis.readLine()) != null ) {
                 L.d( "result" , line);
                 result += line;
             }
             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();
                 }
             }
         }
         return result;
     }
 
     /**
      * 执行命令但不关注结果输出
      */
     public static int execRootCmdSilent(String cmd) {
         int result = - 1 ;
         DataOutputStream dos = null ;
 
         try {
             Process p = Runtime.getRuntime().exec( "su" );
             dos = new DataOutputStream(p.getOutputStream());
 
             L.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();
                 }
             }
         }
         return result;
     }
 
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值