android SD卡自動升級流程

SD卡自動升級即把升級包拷到SD卡後,一點button自動完成所有升級的工作,

不用再手動去點選升級包,主要涉及到三個步驟:

1.檢測升級包是否存在

2.將升級包所在的路徑保存到cache中,重啓進入recovery模式

3.取出升級包路徑,根據升級包路徑找到升級包,完成升級工作.

1.檢測升級包是否存在

   先確認SD卡是否存在:

	public boolean checkSDExist(){
		  if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) 
			{
				return true;
			}
      	
      	return false;
      	
      }

再判斷升級包是否存在

String sdcardPath = null;
String defaultPath = null;
	 StorageManager storageManager = StorageManager.from(this);
	 final StorageVolume[] volumes = storageManager.getVolumeList();
	  for(StorageVolume volume : volumes){
	    	if(volume.isRemovable()){
	    	sdcardPath = volume.getPath();
	    	}else{
	    	      defaultPath = volume.getPath();
	    	     }
	    	}
	    	File file = new File(sdcardPath);
	    	if(sdcardPath==null){
	    				
	    	}
                File[] mfilelist = file.listFiles();
	    	if(mfilelist == null){
	    	mHandler.sendMessage(mHandler.obtainMessage(MSG_ZIP_NOT_EXIST));
	    	break;
	    	}
	    			
	    	for(int i=0;i<mfilelist.length;i++){
	    		String mfileName = mfilelist[i].toString();
	    		if((sdcardPath+"/update.zip").equals(mfileName)){
	    		mHavaZip = true;
	    		}
	    	}


再把路徑傳到rebootrecovery裏面進行處理

		Intent rebootRecovery = new Intent("android.intent.action.UPDATE_SYSTEM");
		rebootRecovery.putExtra("mfiledir", mUpdateDir);

2.將升級包所在的路徑保存到cache中,重啓進入recovery模式

    private static void bootCommand(Context context, String arg) throws IOException {
        RECOVERY_DIR.mkdirs();  //  private static File RECOVERY_DIR = new File("/cache/recovery");
        COMMAND_FILE.delete();  // private static File COMMAND_FILE = new File(RECOVERY_DIR, "command");
        LOG_FILE.delete();

        FileWriter command = new FileWriter(COMMAND_FILE);
        try {
            command.write(arg);
            command.write("\n");
        } finally {
            command.close();
        }

        // Having written the command file, go ahead and reboot
        PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        pm.reboot("recovery");

        throw new IOException("Reboot failed (no permissions?)");
    }

3.取出升級包路徑,根據升級包路徑找到升級包,完成升級工作.

獲取升級包路徑:

 const char *update_package = NULL;
    int wipe_data = 0, wipe_cache = 0, show_text = 0;
    bool just_exit = false;

    int arg;
    while ((arg = getopt_long(argc, argv, "", OPTIONS, NULL)) != -1) {
        switch (arg) {
        case 'p': previous_runs = atoi(optarg); break;
        case 's': send_intent = optarg; break;
        case 'u': update_package = optarg; break;
        case 'w': wipe_data = wipe_cache = 1; break;
        case 'c': wipe_cache = 1; break;
        case 't': show_text = 1; break;
        case 'x': just_exit = true; break;
        case 'l': locale = optarg; break;
        case '?':
            LOGE("Invalid command argument\n");
            continue;
        }
    }
安裝升級包:

        status = install_package(update_package, &wipe_cache, TEMPORARY_INSTALL_FILE);

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章