借助谷歌,并经过本大侠施展坑、蒙、拐、骗、偷五大绝技,终于成功实现在Delphi下获取Root权限并将其扩展为一个完整功能更加完整的TQAndroidShell记录,在华为荣耀2(Android 4.2)、Nubia Z5S(Android 4.4)测试通过。参考代码如下:

 
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
unit QAndroid.Shell;
 
interface
 
{
  本源码来自QDAC项目,版权归swish(QQ:109867294)所有。
  (1)、使用许可及限制
  您可以自由复制、分发、修改本源码,但您的修改应该反馈给作者,并允许作者在必要时,
  合并到本项目中以供使用,合并后的源码同样遵循QDAC版权声明限制。
  您的产品的关于中,应包含以下的版本声明:
  本产品使用的QAndroidShell来自QDAC项目,版权归作者所有。
  (2)、技术支持
  有技术问题,您可以加入QDAC官方QQ群250530692共同探讨。
  (3)、赞助
  您可以自由使用本源码而不需要支付任何费用。如果您觉得本源码对您有帮助,您可以赞
  助本项目(非强制),以使作者不为生活所迫,有更多的精力为您呈现更好的作品:
  赞助方式:
  支付宝: guansonghuan@sina.com 姓名:管耸寰
  建设银行:
  户名:管耸寰
  账号:4367 4209 4324 0179 731
  开户行:建设银行长春团风储蓄所
}
{
  本单元实现了一个伪的Shell,可以用于在Android程序中执行Shell命令,如果需要Root权限,
  则在执行前,调用AskForRoot方法来获取用户的Root授权,如果不再需要在Root账号下执行
  命令,则调用ExitRoot方法来退出Root账号返回普通账号模式。
  1、调用TQAndroidShell.Initialize方法来初始化当前实例
  AShell.Initialize;
  2、如果需要Root权限执行某些命令,调用TQAndroidShell.AskForRoot获得Root权限,否则忽略此步骤
  if AShell.AskForRoot then
  begin
  ...
  end
  else //进入失败,手机未Root或者用户拒绝给予Root权限
  ...;
  3、调用Execute方法来执行命令行并获得返回的结果
  AShell.Execute('ls /proc -l');
  4、如果要切换回普通账号模式,调用AShell.ExitRoot来返回当前普通账号模式
  【注意】TQAndroidShell是一个记录,不需要手工释放(除非你是New生成的)
 
}
 
uses System.SysUtils, System.Diagnostics, Androidapi.Jni,
  Androidapi.JNIBridge,
  Androidapi.Jni.GraphicsContentViewText,
  Androidapi.Jni.JavaTypes,
  Androidapi.Helpers,
  FMX.Helpers.Android, FMX.Forms, FMX.Dialogs, qstring;
 
type
  PQAndroidShell = ^TQAndroidShell;
 
  TQAndroidShell = record
  private
    FRuntime: JObject;
    FProcess: JObject;
    FInRoot: Boolean;
    function GetIsRooted: Boolean;
    function InternalReadReply(AProcess: JObject; ATimeout: Cardinal): QStringW;
    /// <summary>发送一个命令行</summary>
    /// <param name="ACmdline">要发送的命令行内容</param>
    /// <returns>成功,返回true,失败,返回false</returns>
    /// <remarks>仅用于Root模式下</remarks>
    function SendCmd(const ACmdline: QStringW): Boolean;
    function ReadReply(ATimeout: Cardinal = INFINITE): QStringW;
  public
    /// <summary>初始化函数,用于初始化一个TQAndroidShell实例</summary>
    /// <returns>返回当前记录的指针</returns>
    function Initliaize: PQAndroidShell;
    /// <summary>请求Root权限以进入Root账号准备执行后续的命令</summary>
    /// <returns>成功,返回True,失败,返回False</returns>
    /// <remarks>如果失败,一般有两种可能:
    /// 1、设备未Root,此时IsRooted属性为False;
    /// 2、用户拒绝授权
    /// </remarks>
    function AskForRoot: Boolean;
    /// <summary>执行指定的命令行并等待返回</summary>
    /// <param name="ACmdline">命令行内容</param>
    /// <param name="ATimeout">等待命令执行前等待的时间,单位为毫秒</param>
    /// <returns>返回命令行的输出结果</returns>
    function Execute(const ACmdline: QStringW; ATimeout: Cardinal = INFINITE)
      : QStringW;
    /// <summary>退出Root模式</summary>
    /// <remarks>如果未处于Root模式,什么也不会发生</remarks>
    procedure ExitRoot;
    /// <summary>当前是否处于Root模式</summary>
    property InRoot: Boolean read FInRoot;
    /// <summary>当前设备是否已经Root过了</summary>
    property IsRooted: Boolean read GetIsRooted;
  end;
 
implementation
 
type
  JProcess = interface;
  JRuntime = interface;
 
  // ----------------------------------JProcess----------------------
  JProcessClass = interface(JObjectClass)
    ['{7BFD2CCB-89B6-4382-A00B-A7B5BB0BC7C9}']
 
  end;
 
  [JavaSignature('java/lang/Process')]
  JProcess = interface(JObject)
    ['{476414FD-570F-4EDF-B678-A2FE459EA6EB}']
    { Methods }
    procedure destroy; cdecl;
    function exitValue: integer; cdecl;
    function getErrorStream: JInputStream; cdecl;
    function getInputStream: JInputStream; cdecl;
    function getOutputStream: JOutputStream; cdecl;
    function waitFor: integer; cdecl;
  end;
 
  TJProcess = class(TJavaGenericImport<JProcessClass, JProcess>)
  end;
 
  // ----------------------------------Jruntime----------------------
  JRuntimeClass = interface(JObjectClass)
    ['{3F2E949D-E97C-4AD8-B5B9-19CB0A6A29F3}']
    { costant }
  end;
 
  [JavaSignature('java/lang/Runtime')]
  JRuntime = interface(JObject)
    ['{C097A7EC-677B-4BCB-A4BD-7227160750A5}']
    { Methods }
    procedure addShutdownHook(hook: JThread); cdecl;
    function availableProcessors: integer; cdecl;
    function exec(progArray, envp: array of JString): JProcess; overload; cdecl;
    function exec(progArray: JString; envp: array of JString; directory: JFile)
      : JProcess; overload; cdecl;
    function exec(progArray, envp: array of JString; directory: JFile)
      : JProcess; overload; cdecl;
    function exec(prog: JString; envp: array of JString): JProcess; cdecl;
      overload; cdecl;
    function exec(progArray: array of JString): JProcess; overload; cdecl;
    function exec(prog: JString): JProcess; cdecl; overload; cdecl;
    procedure Exit(code: integer); cdecl;
    function freeMemory: LongInt; cdecl;
    procedure gc; cdecl;
    function getLocalizedInputStream(stream: JInputStream): JInputStream; cdecl;
    function getLocalizedOutputStream(stream: JOutputStream)
      : JOutputStream; cdecl;
    function getRuntime: JRuntime; cdecl;
    procedure halt(code: integer); cdecl;
    procedure load(pathName: JString); cdecl;
    procedure loadLibrary(libName: JString); cdecl;
    function maxMemory: LongInt; cdecl;
    function RemoveShutdownHook(hook: JThread): Boolean; cdecl;
    procedure runFinalization; cdecl;
    procedure runFinalizersOnExit(run: Boolean); cdecl;
    function totalMemory: LongInt; cdecl;
    procedure traceInstructions(enable: Boolean); cdecl;
    procedure traceMethodCalls(enable: Boolean); cdecl;
  end;
 
  TJRuntime = class(TJavaGenericImport<JRuntimeClass, JRuntime>)
  end;
 
  { TQAndroidShell }
 
function TQAndroidShell.AskForRoot: Boolean;
begin
Result := InRoot;
if not Result then
  begin
  Result := IsRooted;
  if not Assigned(FProcess) then
    begin
    Result := False;
    if IsRooted then
      begin
      FProcess := (FRuntime as JRuntime).exec(StringToJString('su'));
      if Assigned(FProcess) then
        begin
        // 通过检查当前账号来判断下自己是否成功获取root权限
        if SendCmd('id -nu') then
          begin
          FInRoot := StrStrW(PQCharW(ReadReply), 'root') <> nil;
          Result := FInRoot;
          end;
        end;
      if not Result then
        FProcess := nil;
      end;
    end;
  end;
end;
 
function TQAndroidShell.Initliaize: PQAndroidShell;
begin
FRuntime := TJRuntime.Create;
FProcess := nil;
FInRoot := False;
Result := @Self;
end;
 
function TQAndroidShell.Execute(const ACmdline: QStringW; ATimeout: Cardinal)
  : QStringW;
var
  AProcess: JProcess;
begin
SetLength(Result, 0);
if InRoot then
  begin
  if SendCmd(ACmdline) then
    Result := ReadReply(ATimeout);
  end
else
  begin
  AProcess := (FRuntime as JRuntime).exec(StringToJString(ACmdline));
  if Assigned(AProcess) then
    Result := InternalReadReply(AProcess, ATimeout);
  end;
end;
 
procedure TQAndroidShell.ExitRoot;
begin
if InRoot then
  begin
  if SendCmd('exit') then
    begin
    (FProcess as JProcess).waitFor;
    FProcess := nil;
    FInRoot := False;
    end;
  end;
end;
 
function TQAndroidShell.GetIsRooted: Boolean;
begin
Result := FileExists('/system/bin/su') or FileExists('/system/xbin/su');
end;
 
function TQAndroidShell.InternalReadReply(AProcess: JObject; ATimeout: Cardinal)
  : QStringW;
var
  AError, AInput, AResultStream: JInputStream;
  AWatch: TStopWatch;
  ABuf: TJavaArray<Byte>;
begin
AError := (AProcess as JProcess).getErrorStream;
AInput := (AProcess as JProcess).getInputStream;
AWatch := TStopWatch.StartNew;
AResultStream := nil;
repeat
  if AInput.available > 0 then
    AResultStream := AInput
  else if AError.available > 0 then
    AResultStream := AError;
until (AWatch.ElapsedMilliseconds > ATimeout) or (AResultStream <> nil);
if Assigned(AResultStream) then
  begin
  ABuf := TJavaArray<Byte>.Create(AResultStream.available);
  try
    AResultStream.read(ABuf);
    Result := qstring.Utf8Decode(PQCharA(ABuf.Data), ABuf.Length);
  finally
    FreeAndNil(ABuf);
  end;
  end
else
  SetLength(Result, 0);
end;
 
function TQAndroidShell.ReadReply(ATimeout: Cardinal): QStringW;
begin
Result := InternalReadReply(FProcess, ATimeout);
end;
 
function TQAndroidShell.SendCmd(const ACmdline: QStringW): Boolean;
var
  S: QStringA;
  ABuf: TJavaArray<Byte>;
  AStream: JOutputStream;
begin
S := qstring.Utf8Encode(ACmdline + SLineBreak);
ABuf := TJavaArray<Byte>.Create(S.Length);
try
  Move(PQCharA(S)^, ABuf.Data^, S.Length);
  AStream := (FProcess as JProcess).getOutputStream;
  AStream.write(ABuf);
  AStream.flush;
  Result := True;
except
  Result := False;
end;
FreeAndNil(ABuf);
end;
 
end.

 【提示】应修改AndroidManifest.template.xml文件,加入ACCESS_SUPERUSER权限,虽然不加暂时也没有太大的副作用。

 
1
2
3
4
5
6
    <uses-sdk android:minSdkVersion="%minSdkVersion%" android:targetSdkVersion="%targetSdkVersion%" />
<%uses-permission%>
<!-- 添加此行此声明需要管理员权限 -->
<uses-permission android:name="android.permission.ACCESS_SUPERUSER" />
<!-- 添加完毕 -->
    <application android:persistent="%persistent%"

不需多说,看疗效:

   

完整的源码下载:百度网盘

http://www.qdac.cc/?p=1130

Delphi编写的Android程序获取Root权限实现(2015.4.15更新,支持Android 4.4)的更多相关文章

  1. android中获取root权限的方法以及原理(转)

    一. 概述 本文介绍了android中获取root权限的方法以及原理,让大家对android 玩家中常说的“越狱”有一个更深层次的认识. 二. Root 的介绍 1. Root 的目的 可以让我们拥有 ...

  2. Android 获取ROOT权限原理解析

    一. 概述 本文介绍了android中获取root权限的方法以及原理,让大家对android玩家中常说的“越狱”有一个更深层次的认识. 二. Root的介绍 1.       Root 的目的 可以让 ...

  3. Android 上SuperUser获取ROOT权限原理解析

    Android 上SuperUser获取ROOT权限原理解析 一. 概述 本文介绍了android中获取root权限的方法以及原理,让大家对android 玩家中常说的“越狱”有一个更深层次的认识. ...

  4. 获取root权限及破解原理分析

    2012-03-18 17:58:45|  分类: android |字号 订阅 如今Android系统的root破解基本上成为大家的必备技能!网上也有非常多中一键破解的软件,使root破解越来越ea ...

  5. Android -- 程序判断手机ROOT状态,获取ROOT权限

    判断手机是否具有ROOT权限                                                            /** * 判断手机是否ROOT */ public ...

  6. 怎样使Android应用程序获得root权限

    Normal 0 7.8 磅 0 2 false false false MicrosoftInternetExplorer4 写这篇文章前,首先要感谢 Simon_fu ,他的两篇关于 root 权 ...

  7. [Android Pro] Android fastboot刷机和获取Root权限

    参考文章: https://developers.google.com/android/nexus/images 转载自:    http://www.inexus.co/article-1280-1 ...

  8. Android获取Root权限之后的静默安装实现代码示例分析

    转:http://blog.csdn.net/jiankeufo/article/details/43795015 Adroid开发中,我们有时会遇到一些特殊功能的实现,有些功能并没有太高技术难度,但 ...

  9. Android应用请求获取Root权限

    应用获取Root权限的原理:让应用的代码执行目录获取最高权限.在Linux中通过chmod 777 [代码执行目录] /** * 应用程序运行命令获取 Root权限,设备必须已破解(获得ROOT权限) ...

随机推荐

  1. Android 有用的快捷键

    The powerful Android Studio 08 Jun 2016 Android Studio is the official tool for Android development ...

  2. Mac ssh登陆linux并且显示linux图形

    背景: Mac 通过[终端]ssh登陆linux并且在Mac显示linux图形 Mac 主机IP: 10.2.1.1 linux 主机IP: 192.168.1.1 说明: 想要ssh访问并且显示li ...

  3. Android Studio .grade文件知识

    1.位置 2.问题:当AndroidStudio版本不同的时候,导致.gradle文件版本不同(当前为2.10),因为AndroidStudio升级的时候会自动升级.gradle.这样通过svn加载到 ...

  4. 求绝对值,hdu-2003

    求绝对值 Problem Description 求实数的绝对值.   Input 输入数据有多组,每组占一行,每行包含一个实数.   Output 对于每组输入数据,输出它的绝对值,要求每组数据输出 ...

  5. java 编码 UTF-8、ISO-8859-1、GBK 【转】

    Java支持UTF-8.ISO-8859-1.GBK等各种字体编码,可笔者发现Java中字体编码的问题仍难倒了不少程序员,网上虽然也有不少关于在Java中如何正确显示中文的文章,但都不够全面,笔者特意 ...

  6. 【写一个自己的js库】 4.完善跨浏览器事件操作

    1.阻止冒泡. function stopPropagation(event){ event = event || getEvent(event); if(event.stopPropagation) ...

  7. linux scp ssh命令不用输入密码

    把你的本地主机用户的ssh公匙文件复制到远程主机用户的~/.ssh/authorized_keys文件中 假设本地主机linux100,远程主机linux200 一,在linux100主机里的用户 运 ...

  8. JAVA反射学习网摘

    程序运行时,允许改变程序结构或变量类型,这种语言称为动态语言".从这个观点看,Perl,Python,Ruby是动态语言,C++,Java,C#不是动态语言.但是JAVA有着一个非常突出的动 ...

  9. 2.4 statistical decision theory

    在讲完最小二乘(linear regression)和K近邻后,进入本节. 引入符号: $X\in R^p$ X为维度为p的输入向量 $Y\in R$ Y为输出,实数 $P(X,Y)$ 为两者的联合概 ...

  10. Median of Two Sorted 求两个有序数组的中位数

    中位数是把一个数的集合划分为两部分,每部分包含的数字个数相同,并且一个集合中的元素均大于另一个集合中的元素. 因此,我们考虑在一个任意的位置,将数组A划分成两部分.i表示划分数组A的位置,如果数组A包 ...