產品需求,木有辦法。android系統是跟廠商定制的,保證系統開機就運行我們的app,並且實現自己靜默安裝,完全自動化,無需人工操作。
網上有很多辦法,
1、要么要通過android 源碼拿到密鑰文件,參考:http://www.cnblogs.com/brucenan/archive/2012/10/04/2711817.html
2、通過root權限,可以參考代碼(已測試安裝別人的apk可以,但是不能安裝自己):
import java.io.File; import java.io.PrintWriter; import android.content.Context; import android.content.Intent; import android.net.Uri; import android.util.Log; public class ApkController { /** * 描述: 安裝 */ public static boolean install(String apkPath,Context context){ // 先判斷手機是否有root權限 if(hasRootPerssion()){ // 有root權限,利用靜默安裝實現 return clientInstall(apkPath); }else{ // 沒有root權限,利用意圖進行安裝 File file = new File(apkPath); if(!file.exists()) return false; Intent intent = new Intent(); intent.setAction("android.intent.action.VIEW"); intent.addCategory("android.intent.category.DEFAULT"); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setDataAndType(Uri.fromFile(file),"application/vnd.android.package-archive"); context.startActivity(intent); return true; } } /** * 描述: 卸載 */ public static boolean uninstall(String packageName,Context context){ if(hasRootPerssion()){ // 有root權限,利用靜默卸載實現 return clientUninstall(packageName); }else{ Uri packageURI = Uri.parse("package:" + packageName); Intent uninstallIntent = new Intent(Intent.ACTION_DELETE,packageURI); uninstallIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(uninstallIntent); return true; } } /** * 判斷手機是否有root權限 */ public static boolean hasRootPerssion(){ PrintWriter PrintWriter = null; Process process = null; try { process = Runtime.getRuntime().exec("su"); PrintWriter = new PrintWriter(process.getOutputStream()); PrintWriter.flush(); PrintWriter.close(); int value = process.waitFor(); return returnResult(value); } catch (Exception e) { e.printStackTrace(); }finally{ if(process!=null){ process.destroy(); } } return false; } /** * 靜默安裝 */ public static boolean clientInstall(String apkPath){ PrintWriter PrintWriter = null; Process process = null; try { process = Runtime.getRuntime().exec("su"); PrintWriter = new PrintWriter(process.getOutputStream()); PrintWriter.println("chmod 777 "+apkPath); PrintWriter.println("export LD_LIBRARY_PATH=/vendor/lib:/system/lib"); PrintWriter.println("pm install -r "+apkPath); // PrintWriter.println("exit"); PrintWriter.flush(); PrintWriter.close(); int value = process.waitFor(); return returnResult(value); } catch (Exception e) { e.printStackTrace(); }finally{ if(process!=null){ process.destroy(); } } return false; } /** * 靜默卸載 */ public static boolean clientUninstall(String packageName){ PrintWriter PrintWriter = null; Process process = null; try { process = Runtime.getRuntime().exec("su"); PrintWriter = new PrintWriter(process.getOutputStream()); PrintWriter.println("LD_LIBRARY_PATH=/vendor/lib:/system/lib "); PrintWriter.println("pm uninstall "+packageName); PrintWriter.flush(); PrintWriter.close(); int value = process.waitFor(); return returnResult(value); } catch (Exception e) { e.printStackTrace(); }finally{ if(process!=null){ process.destroy(); } } return false; } /** * 啟動app * com.exmaple.client/.MainActivity * com.exmaple.client/com.exmaple.client.MainActivity */ public static boolean startApp(String packageName,String activityName){ boolean isSuccess = false; String cmd = "am start -n " + packageName + "/" + activityName + " \n"; Process process = null; try { process = Runtime.getRuntime().exec(cmd); int value = process.waitFor(); return returnResult(value); } catch (Exception e) { e.printStackTrace(); } finally{ if(process!=null){ process.destroy(); } } return isSuccess; } private static boolean returnResult(int value){ // 代表成功 if (value == 0) { return true; } else if (value == 1) { // 失敗 return false; } else { // 未知情況 return false; } } }
3、通過走系統應用的方法(因為機器是我們定制生產,所以可以將app預置到system/app目錄下,無需安裝,直接重啟,可以看到系統再次開機時會顯示系統升級,然后就可以看到我們的app安裝成功),可以試着先adb命令執行,再復制到代碼中去執行。注意,如果app的lib中有.so文件,則還需要將.so文件拷貝到system/libs目錄,否則報錯(我的解決辦法是暫時將用到.so文件的地方注釋掉,等有時間再做)
adb 命令:
$ adb push SecureSetting.apk /sdcard/ // 上傳要安裝的文件,為安裝做准備。 $ adb shell $ su // 切換到 root 用戶。如果沒有獲得 Root 權限,這一步不會成功。 # mount //顯示當前mount情況 # mount -o remount,rw -t yaffs2 /dev/block/mtdblock3 /system // 讓分區可寫。 # cat /sdcard/SecureSetting.apk > /system/app/SecureSetting.apk // 這一步可以用 cp 實現,但一般設備中沒有包含該命令。如果使用 mv 會出現錯誤:failed on '/sdcard/NetWork.apk' - Cross-device link。 chmod 777 chetou.apk # mount -o remount,ro -t yaffs2 /dev/block/mtdblock3 /system // 還原分區屬性,只讀。 # exit $ exit
代碼執行:
/** * 將文件復制到system/app 目錄 * @param apkPath 特別注意格式:該路徑不能是:/storage/emulated/0/app/QDemoTest4.apk 需要是:/sdcard/app/QDemoTest4.apk * @return */ public static boolean copy2SystemApp(String apkPath){ PrintWriter PrintWriter = null; Process process = null; String appName = "test.apk",cmd; try { process = Runtime.getRuntime().exec("su"); PrintWriter = new PrintWriter(process.getOutputStream()); cmd = "mount -o remount,rw -t yaffs2 /dev/block/mtdblock3 /system"; Log.e("copy2SystemApp", cmd); PrintWriter.println(cmd); cmd = "cat "+apkPath+" > /system/app/"+appName; Log.e("copy2SystemApp", cmd); PrintWriter.println(cmd); cmd = "chmod 777 /system/app/"+appName +" -R"; Log.e("copy2SystemApp", cmd); PrintWriter.println(cmd); cmd = "mount -o remount,ro -t yaffs2 /dev/block/mtdblock3 /system"; Log.e("copy2SystemApp", cmd); PrintWriter.println(cmd); PrintWriter.println("reboot"); //重啟 PrintWriter.println("exit"); PrintWriter.flush(); PrintWriter.close(); int value = process.waitFor(); return returnResult(value); } catch (Exception e) { e.printStackTrace(); }finally{ if(process!=null){ process.destroy(); } } return false; }
注意代碼里有差異性,因為需要給文件 chmod 777權限,否則不會自動升級安裝。流程可以參考:http://blog.163.com/wwj0925@126/blog/static/19120110201522242241563/
