編輯:關於 Android 程式設計
本文說明如何利用 root 權限靜默安裝 APK、如何自動選擇「普通安裝」或「靜默安裝」,以及如何擴展 PackageUtils 來實作靜默解除安裝。
直接呼叫 PackageUtils.installSilent()。系統的權限管理會彈出對話框,詢問使用者是否允許應用取得 root 權限;一旦允許,後續即可靜默安裝。
該函式成功時回傳 PackageUtils.INSTALL_SUCCEEDED,失敗則回傳對應的錯誤碼,可以據此得知詳細原因——檔案不存在、apk 無效、系統空間不足、簽名不正確、缺少共用函式庫、share user 錯誤等等。
注意:較大的 apk 安裝過程非常耗時,務必另開執行緒呼叫,不要阻塞主執行緒。
PackageUtils.installSilent() 本質上就是執行 su 取得 root 後,再呼叫 pm install -r filePath。核心程式碼如下:
public class ShellUtils {
public static final String COMMAND_SU = "su";
public static final String COMMAND_SH = "sh";
public static final String COMMAND_EXIT = "exit\n";
public static final String COMMAND_LINE_END = "\n";
public static CommandResult execCommand(String[] commands,
boolean isRoot,
boolean isNeedResultMsg) {
int result = -1;
if (commands == null || commands.length == 0) {
return new CommandResult(result, null, null);
}
Process process = null;
BufferedReader successResult = null;
BufferedReader errorResult = null;
StringBuilder successMsg = null;
StringBuilder errorMsg = null;
DataOutputStream os = null;
try {
process = Runtime.getRuntime().exec(isRoot ? COMMAND_SU : COMMAND_SH);
os = new DataOutputStream(process.getOutputStream());
for (String command : commands) {
if (command == null) {
continue;
}
// 不要用 os.writeBytes(command),會有中文編碼問題
os.write(command.getBytes());
os.writeBytes(COMMAND_LINE_END);
os.flush();
}
os.writeBytes(COMMAND_EXIT);
os.flush();
result = process.waitFor();
if (isNeedResultMsg) {
successMsg = new StringBuilder();
errorMsg = new StringBuilder();
successResult = new BufferedReader(
new InputStreamReader(process.getInputStream()));
errorResult = new BufferedReader(
new InputStreamReader(process.getErrorStream()));
String s;
while ((s = successResult.readLine()) != null) {
successMsg.append(s);
}
while ((s = errorResult.readLine()) != null) {
errorMsg.append(s);
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (os != null) os.close();
if (successResult != null) successResult.close();
if (errorResult != null) errorResult.close();
} catch (IOException e) {
e.printStackTrace();
}
if (process != null) {
process.destroy();
}
}
return new CommandResult(result,
successMsg == null ? null : successMsg.toString(),
errorMsg == null ? null : errorMsg.toString());
}
}
其中 commands 就是 pm install -r <path>。可見關鍵在於:先用 su 切換到 root 環境,再執行 pm install。
PackageUtils 另外提供了 install(),它會依「是否為系統應用」以及「是否已取得 root」來決定要走哪一條路:
/**
* install according conditions
* if system application or rooted, see installSilent()
* else see installNormal()
*/
public static final int install(Context context, String filePath) {
if (!PackageUtils.isSystemApplication(context)) {
boolean isRoot = ShellUtils.checkRootPermission();
if (!isRoot) {
return installNormal(context, filePath)
? INSTALL_SUCCEEDED
: INSTALL_FAILED_INVALID_URI;
}
}
return installSilent(context, filePath);
}
判斷邏輯是:非系統應用且未取得 root → 走普通安裝(跳系統安裝器);否則 → 走靜默安裝。這樣呼叫端就不必自己判斷環境。
原理完全相同,把指令換成 pm uninstall [-k] PACKAGE 即可(-k 表示保留資料與快取目錄)。
Runtime.exec("su") 會直接失敗,唯一能用的只有普通安裝(意圖跳安裝器)。PackageInstaller.Session,可在不需 root 的前提下靜默安裝(仍需 REQUEST_INSTALL_PACKAGES 權限並引導使用者開啟「允許未知來源」)。這才是官方支援的路徑。
Android 圖片載入框架 Universal-Image-Loader 詳解
若要票選最廣泛被使用的圖片載入函式庫,Universal-Image-Loader(以下簡稱 UIL)大概無出其右。隨便反編譯幾個主流 App,幾乎都能看到它的身影,像個
Android 多點觸控 MultiTouch 淺析
前言 本文說明如何在應用層支援多點觸控操作。對於常規控件的觸控,實現 onTouchEvent() 方法來處理即可;同時也會對 onTouchEvent 的參數 Moti
Android PopupWindow 實作:多層級聯動篩選選單
在不少列表頁與搜尋場景裡,我們會看到「一級選單切換、二級選項連動更新」的篩選設計。本文以 PopupWindow 搭配雙 ListView 為基礎,示範如何打造這樣一個層
Android 懸浮相機窗口實戰:WindowManager + Service + Camera 全流程
懸浮窗裡嵌一個相機預覽,是很多工具類應用的常見需求,比如掃碼懸浮入口、遠程監控小窗、拍照打卡等。本文把這個功能的完整鏈路走一遍:用 WindowManager 把一個極小