Android OTA本地自动升级实现

版权声明:本文为博主原创文章,未经博主允许不得转载。转载请务必加上转载https://blog.csdn.net/qq_23327993 https://blog.csdn.net/qq_23327993/article/details/89955990

前言

上面在Android ota 升级方式中介绍了几种方式,这里详细介绍下本地自动升级方式,话不多说,现在开始…

正文

本文基于qcom msm8953 Android7.1.2平台。
ota升级首先的准备升级包,这里命名为update.zip。不知道OTA升级包怎么制作的小伙伴,可以戳这里~
这里,我们假设已经拿到了升级包update.zip。
开始撸代码,那么,做到这里,你们应该知道Android 系统提供了一些api供我们来调用,具体表现在android.os.RecoverySystem类中的installPackage(Context context, File packageFile).
该类中还有很多方法,其中有个verifyPackage方法得提下,该方法用来验证升级包的,虽然在recovery系统中也会去验证升级包,这里还是建议在应用层也做下升级包的验证,可以提前规避错误,该类的其他方法大家可以阅读下Android开发文档,这里需要梯子哟
在这里插入图片描述
在这里插入图片描述
这里简单上下代码,主要看看这两个方法如何使用:

public class SdCardUpgradeProcess extends Activity implements RecoverySystem.ProgressListener {

private static final String TAG = "SdCardUpgrade";
private static final int VERIFY_COMPLETE = 70;
private static final int INSTALL_COMPLETE  = 100;
private static String updatePath = "/data/ota_package/";
private String updateName = "";
private ProgressBar mProcessbar;
private TextView mUpdateStep;
private TextView mUpdateState;
private TextView mNotify;
private TextView sdcard_update_introduction_textview_one;
private TextView sdcard_update_introduction_textview_two;
private ImageView mCompleteImg;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.sdcardupgrade_processbar);

    UpdateInfo updateInfo = getIntent().getParcelableExtra("updateInfo");
    Log.e(TAG,"name:"+updateInfo.getUpdateName());
    Log.e(TAG,"cnt:"+updateInfo.getUpdateCnt());
    updateName = updateInfo.getUpdateName();

    mUpdateStep = (TextView)findViewById(R.id.step_number);
    mUpdateState = (TextView)findViewById(R.id.processbar_title);
    mNotify = (TextView)findViewById(R.id.update_notify);
    mCompleteImg = (ImageView)findViewById(R.id.update_complete);

    mProcessbar=(ProgressBar)findViewById(R.id.processbar);
    mProcessbar.setMax(110);
    mProcessbar.setProgress(0);
    mProcessbar.setIndeterminate(false);

    runnable.start();

}
Handler mHandler = new Handler(){
    public void handleMessage(Message msg) {

        mProcessbar.setProgress(msg.arg1);

        switch(msg.arg1){
            case VERIFY_COMPLETE:
                mUpdateStep.setText(getString(R.string.the_next_step_number));
                mUpdateState.setText(getString(R.string.install_process));
                mNotify.setText("");
                break;

            case INSTALL_COMPLETE:
                mUpdateState.setText(getString(R.string.install_process_complete));
                mNotify.setText(getString(R.string.restart));
                mCompleteImg.setBackgroundResource(R.drawable.ic_launcher_background);
                break;

            default:
                break;
        }
    }
};
Thread runnable = new Thread(){

    @Override
    public void run() {
        if(null == updatePath || null == updateName)
            return;
        Log.d(TAG, "Start update .............");
        File file = new File(updatePath+updateName);
        Log.d(TAG, "file:" +file+"");
        PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
        PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "SdCardUpgrade ProcessBar");
        try{
            wl.acquire();//升级保持亮屏状态
            RecoverySystem.verifyPackage(file, SdCardUpgradeProcess.this, null);
            Log.d(TAG,"Verify package complete.");
            RecoverySystem.installPackage(SdCardUpgradeProcess.this, file);

        }catch(Exception e){
            Log.e(TAG, e.getMessage(), e);

        }finally{
            wl.release();
        }
    }
};

@Override
public void onProgress(int progress) {
    Log.d(TAG,"progress="+progress);
    Message msg = Message.obtain();
    msg.arg1 = progress;
    mHandler.sendMessage(msg);
}
@Override
public void onBackPressed() {
    // TODO Auto-generated method stub
    System.exit(0);
}

}

P.S

完整的demo下载路径:AndroidOtaUpdate.rar

猜你喜欢

转载自blog.csdn.net/qq_23327993/article/details/89955990