linxu上批處理命令的簡單介紹
批處理命令格式:不同命令之間用“;”或“&&”隔開
要實現(xiàn)靜默安裝其實就是adb的2個命令拼接,并用“;”或“&&”隔開
- adb 卸載app的命令:
pm uninstall 包名 - adb覆蓋安裝命令:
pm install -rtd apk文件(包含路徑) - adb啟動activity的命令:
am start -n 包名/.包下面的主activity
調(diào)用方法靜默安裝的方法
String apkPath = apk文件(包含路徑);
String runApp = "am start -n 包名/.包下的啟動activity >/dev/null 2>&1;";
String install = "pm install -rtd " + apkPath + " >/dev/null 2>&1; ";
String bat = install + runApp;
Log.i(TAG,"linxu bat :"+bat);
SystemManager.RootCommand(bat);
普通實現(xiàn)辦法是通過linux的批處理命令處理
pm install -rtd /storage/emulated/0/apkpaht/apkfile;am start -n com.***.***/.activity.LaunchActivity
本來通過這個方法一切正常,安裝更新后可以再次啟動apk
最近在某些模擬器上這個方法盡然通不過,現(xiàn)象是apk已經(jīng)更新安裝,不過不能打開新的app
優(yōu)化linux的批處理命令處理
經(jīng)模擬器開放人員修改后的批處理代碼(為批處理添加日志打印),結(jié)果莫名其妙的可以了。(具體原因他也說不清楚)
pm install -rtd /storage/emulated/0/apkpaht/apkfile >sdcard/test.log 2>&1; am start -n com.***.***/.activity.LaunchActivity >>sdcard/test.log 2>&1;
最后使用的批處理命令(把日志打印到一個空設(shè)備上)
經(jīng)模擬器開放人員修改后的批處理代碼(為批處理添加日志打印),結(jié)果莫名其妙的可以了。(具體原因他也說不清楚)
pm install -rtd /storage/emulated/0/apkpaht/apkfile >/dev/null 2>&1; am start -n com.***.***/.activity.LaunchActivity >>/dev/null 2>&1; ;
附:SystemManager工具類
public class SystemManager {
public static void RootCommand(String command) {
Process process = null;
DataOutputStream os = null;
try {
process = Runtime.getRuntime().exec("su");
os = new DataOutputStream(process.getOutputStream());
os.writeBytes(command + "\n");
os.writeBytes("exit\n");
os.flush();
process.waitFor();
} catch (Exception e) {
TogglableLog.e("SystemManager", "command:" + command);
} finally {
try {
if (os != null) {
os.close();
}
if (process != null) {
process.destroy();
}
} catch (Exception e) {
LogUtil.e("SystemManager", e);
}
}
TogglableLog.d("SystemManager", "root success");
}
public static void Root(String command) {
Process process = null;
DataOutputStream os = null;
try {
process = getProcess();
os = new DataOutputStream(process.getOutputStream());
os.writeBytes(command + "\n");
os.writeBytes("exit\n");
os.flush();
process.waitFor();
} catch (Exception e) {
Log.e("Root", "command:" + command);
} finally {
try {
if (os != null) {
os.close();
}
process.destroy();
} catch (Exception e) {
Log.e("Root", "" + e);
}
}
Log.d(" Root", "root success");
}
public static void Root777(String command) {
Process process = null;
DataOutputStream os = null;
try {
process = getProcess();
os = new DataOutputStream(process.getOutputStream());
command = "chmod 777 " + command;
os.writeBytes(command + "\n");
os.writeBytes("exit\n");
os.flush();
process.waitFor();
} catch (Exception e) {
Log.e("Root", "command:" + command);
} finally {
try {
if (os != null) {
os.close();
}
process.destroy();
} catch (Exception e) {
Log.e("Root", "" + e);
}
}
Log.d(" Root", "root success");
}
/**
* root 權(quán)限
*/
public static void suRoot(final Context context) {
try {
TermSessionCommandUtil.getInstance(context).exec("su");
new Thread(new Runnable() {
@Override
public void run() {
authXposedFile2(context);
}
}).start();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 獲取root 權(quán)限
*/
public static Process getProcess() {
Process process = null;
try {
process = Runtime.getRuntime().exec("su");
} catch (IOException e) {
e.printStackTrace();
}
return process;
}
/**
* 手機(jī)是否root
*
* @return
*/
public static boolean isRoot() {
boolean isRoot = false;
String sys = System.getenv("PATH");
ArrayList<String> commands = new ArrayList<String>();
String[] path = sys.split(":");
for (int i = 0; i < path.length; i++) {
String commod = "ls -l " + path[i] + "/su";
commands.add(commod);
System.out.println("commod : " + commod);
}
ArrayList<String> res = run("/system/bin/sh", commands);
String response = "";
for (int i = 0; i < res.size(); i++) {
response += res.get(i);
}
int inavailableCount = 0;
String root = "-rwsr-sr-x root root";
for (int i = 0; i < res.size(); i++) {
if (res.get(i).contains("No such file or directory")
|| res.get(i).contains("Permission denied")) {
inavailableCount++;
}
}
return inavailableCount < res.size();
}
// 批量運(yùn)行命令行
private static ArrayList run(String shell, ArrayList<String> commands) {
ArrayList output = new ArrayList();
Process process = null;
try {
process = Runtime.getRuntime().exec(shell);
BufferedOutputStream shellInput = new BufferedOutputStream(
process.getOutputStream());
BufferedReader shellOutput = new BufferedReader(
new InputStreamReader(process.getInputStream()));
for (String command : commands) {
shellInput.write((command + " 2>&1\n").getBytes());
}
shellInput.write("exit\n".getBytes());
shellInput.flush();
String line;
while ((line = shellOutput.readLine()) != null) {
output.add(line);
}
process.waitFor();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
process.destroy();
}
return output;
}
}