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);

你可能感兴趣的:(android,recovery)