-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cdfcb96
commit 16300ca
Showing
7 changed files
with
192 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
apply plugin: 'com.android.library' | ||
|
||
android { | ||
compileSdkVersion 29 | ||
buildToolsVersion "29.0.0" | ||
defaultConfig { | ||
minSdkVersion 15 | ||
targetSdkVersion 29 | ||
versionCode 1 | ||
versionName "1.0" | ||
} | ||
buildTypes { | ||
release { | ||
minifyEnabled false | ||
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' | ||
} | ||
} | ||
} | ||
|
||
dependencies { | ||
implementation fileTree(dir: 'libs', include: ['*.jar']) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Add project specific ProGuard rules here. | ||
# You can control the set of applied configuration files using the | ||
# proguardFiles setting in build.gradle. | ||
# | ||
# For more details, see | ||
# http://developer.android.com/guide/developing/tools/proguard.html | ||
|
||
# If your project uses WebView with JS, uncomment the following | ||
# and specify the fully qualified class name to the JavaScript interface | ||
# class: | ||
#-keepclassmembers class fqcn.of.javascript.interface.for.webview { | ||
# public *; | ||
#} | ||
|
||
# Uncomment this to preserve the line number information for | ||
# debugging stack traces. | ||
#-keepattributes SourceFile,LineNumberTable | ||
|
||
# If you keep the line number information, uncomment this to | ||
# hide the original source file name. | ||
#-renamesourcefileattribute SourceFile |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<manifest | ||
package="cn.endureblaze.executeshell_lib"> | ||
|
||
<application /> | ||
</manifest> |
139 changes: 139 additions & 0 deletions
139
executeshell_lib/src/main/java/cn/endureblaze/executeshell/ExecuteShell.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
package cn.endureblaze.executeshell; | ||
|
||
import android.util.Log; | ||
|
||
import java.io.DataInputStream; | ||
import java.io.DataOutputStream; | ||
import java.io.IOException; | ||
|
||
public final class ExecuteShell | ||
{ | ||
private static final String TAG = "RootCmd"; | ||
private static boolean mHaveRoot = false; | ||
/** | ||
* 判断机器Android是否已经root,即是否获取root权限 | ||
*/ | ||
public static boolean haveRoot() | ||
{ | ||
if (!mHaveRoot) | ||
{ | ||
int ret = execRootCmdSilent("echo test"); // 通过执行测试命令来检测 | ||
if (ret != -1) | ||
{ | ||
Log.i(TAG, "have root!"); | ||
mHaveRoot = true; | ||
} | ||
else | ||
{ | ||
Log.i(TAG, "not root!"); | ||
} | ||
} | ||
else | ||
{ | ||
Log.i(TAG, "mHaveRoot = true, have root!"); | ||
} | ||
return mHaveRoot; | ||
} | ||
|
||
/** | ||
* 执行命令并且输出结果 | ||
*/ | ||
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()); | ||
|
||
Log.i(TAG, cmd); | ||
dos.writeBytes(cmd + "\n"); | ||
dos.flush(); | ||
dos.writeBytes("exit\n"); | ||
dos.flush(); | ||
String line = null; | ||
while ((line = dis.readLine()) != null) | ||
{ | ||
Log.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()); | ||
|
||
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(); | ||
} | ||
} | ||
} | ||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<resources> | ||
<string name="app_name">executeshell</string> | ||
</resources> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters