第一篇部落格:将使用者程式轉為系統程式
時光飛逝,大三因為喜歡玩手機軟體開始接觸安卓,到現在即将畢業,自己也在試用期中,覺得是時候慢慢把學到的,用到的東西記錄下來,也友善以後項目需要用到進行查找,第一篇來講講本地應用轉為系統應用(前提是得Root,搞機的無視此提醒)。
轉成系統應用原理是使用Java的Runtime.getRuntime().exec(“linux指令”)實作轉換,首先将/system/目錄挂載為可讀寫,然後擷取本地應用的安裝路徑,設定安裝路徑為/system/app/xx.apk,執行cat将本地應用檔案寫到系統應用檔案夾,然後修改xx.apk的權限即可。
```
// 執行root指令的方法
public static boolean 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) {
Log.d("*** DEBUG ***", "ROOT REE" + e.getMessage());
return false;
} finally {
try {
if (os != null) {
os.close();
}
process.destroy();
} catch (Exception e) {
}
}
Log.d("*** DEBUG ***", "Root SUC ");
return true;
}
try {
// 擷取應用安裝的路徑
String sourceDir = context.getPackageManager() .getPackageInfo(context.getPackageName(), ).applicationInfo.sourceDir;
// 安裝目标路徑
String sourceTarget = "/system/app/" + Constant.APP_NAME+ ".apk";
// 挂載系統應用檔案夾可讀寫,寫入
String apkRoot = "mount -o remount,rw /system" + "\n"+ "cat " + sourceDir + " > " + sourceTarget;
// 執行指令
RootCmd.RootCommand(apkRoot);
// 修改權限
String apkRoot1 = "chmod 644 " + sourceTarget;
// 執行指令
RootCmd.RootCommand(apkRoot1);
// 轉換後先驗證是否轉換成功,成功則彈出提示窗
File file = new File(sourceTarget);
if (file.exists()) {
RootCmd.showRebootDialog(context);
} else {
ToastUtil.toast(context, "未轉換成功,是否沒有ROOT或授權該應用");
}
} catch (Exception e) {
e.printStackTrace();
}
在弄畢設的時候做的是程式鎖,為防止解除安裝做了這個功能,在網上找了很多例子都是寫到/system/app/檔案夾後app是錯誤的,這個是自己調試修改代碼修改成功的,在原生系統,MIUI,FIUI,IUNI系統均可以轉成系統應用。