Android OTA local automatically upgraded to achieve

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. Be sure to add reprint reprint https://blog.csdn.net/qq_23327993 https://blog.csdn.net/qq_23327993/article/details/89955990

Foreword

In the above described several ways Android ota upgrade in detail here under local automatic upgrade , did not talk much, now ...

text

Based qcom msm8953 Android7.1.2 platform.
First of ota upgrade package ready to upgrade, here named update.zip. OTA upgrade package did not know how to make a small partner, you can poke here -
here, we assume've got the upgrade package update.zip.
Start line and code, then, to do this, you should know that the Android system offers some installPackage api for us to call, in particular in android.os.RecoverySystem class (Context context, File packageFile).
This class, there are many the method, which was put under a verifyPackage method, the method used to verify the upgrade package, although the recovery system will be to verify the upgrade package, here it is recommended that the upgrade package at the application layer to do the verification, you can avoid errors in advance other methods of the class you can read at the Android developer documentation here need a ladder yo
Here Insert Picture Description
Here Insert Picture Description
here simple up and down the code, mainly to see how the use of these two methods:

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

Complete demo download path: AndroidOtaUpdate.rar

Guess you like

Origin blog.csdn.net/qq_23327993/article/details/89955990