Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android系統SD卡升級

Android系統SD卡升級

編輯:關於Android編程

一般在本地進行OTA升級時,將升級包拷貝到SD卡,然後進入recovery模式選擇升級包進行升級,但不是很方便,於是做了一個小應用,通過調用RecoverySystem類中的接口來進行升級。實際上最終也是在recovery模式下進行升級,只不過是在應用層對升級包進行簽名驗證和發送升級命令。 內容比較簡單直接上代碼。   一、主要類 SdCardUpgradeWarning.java 一個簡單的Activity,對用戶升級前進行一些警告提示。 SdCardUpgradeIntroduction.java 對升級過程一些注意事項的提示。 SdCardUpgradeProcess.java 這個是最主要的一個類,讀取升級包並進行驗證,然後調用installPackage進行升級。

二、詳細代碼

1.AndroidManifest.xml



                  


注意添加android:sharedUserId="android.uid.system",否則一些系統權限無法使用,如重啟。

 

2.SdCardUpgradeWarning.java

public class SdCardUpgradeWarning extends Activity implements View.OnClickListener {

    private static final String TAG = "SdCardUpgradeWarning";

    private static String updatePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/update.zip";
    private Button mCancle;
    private Button mConfirm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sdcardupgrade_warning);
        mCancle = (Button) findViewById(R.id.cancle);
        mCancle.setOnClickListener(this);
        mConfirm = (Button) findViewById(R.id.confirm);
        mConfirm.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.cancle:
                finish();
                break;
            case R.id.confirm:
                confirmUpdatePackageExist();
                break;

            default:
                break;
        }
    }

    public void confirmUpdatePackageExist() {
        File updatePackage = new File(updatePath);
        if (isFileExist(updatePackage)) {
            boolean isZipfileExist = updatePackage.exists();
            if (!isZipfileExist) {
                Toast toast = Toast.makeText(getApplicationContext(), getString(R.string.update_package_not_found), Toast.LENGTH_LONG);
                toast.setGravity(0, 0, 100);
                toast.show();
            } else {
                Intent intent = new Intent();
                intent.setClass(this, SdCardUpgradeIntroduction.class);
                startActivity(intent);
            }

        } else {
            Toast toast = Toast.makeText(getApplicationContext(), getString(R.string.sdcard_not_mounted), Toast.LENGTH_LONG);
            toast.setGravity(0, 0, 100);
            toast.show();
        }
    }

    public boolean isFileExist(File file) {
        if (file.exists()) {
            return true;
        } else {
            Log.d(TAG, "Update zip file not exist.");
            return false;
        }
    }

}
3.SdCardUpgradeIntroduction.java
public class SdCardUpgradeIntroduction extends Activity implements View.OnClickListener {

    private Button mcancle;
    private Button mconfirm;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        setContentView(R.layout.sdcardupgrade_introduction);

        mcancle=(Button)findViewById(R.id.cancle);
        mcancle.setOnClickListener(this);
        mconfirm=(Button)findViewById(R.id.confirm);
        mconfirm.setOnClickListener(this);
    }
    @Override
    public void onClick(View v){
        switch(v.getId()){
            case R.id.cancle:
                finish();
                break;

            case R.id.confirm:
                Intent intent = new Intent();
                intent.setClass(this, SdCardUpgradeProcess.class);
                startActivity(intent);
                break;

            default:
                break;
        }
    }

}
4.SdCardUpgradeProcess.java
public class SdCardUpgradeProcess extends Activity implements RecoverySystem.ProgressListener {
    private static final String TAG = "SdCardUpgradeProcess";
    private static final int VERIFY_COMPLETE = 70;
    private static final int INSTALL_COMPLETE  = 100;
    private static String updatePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/update.zip";

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

        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.update_complete);
                    break;

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

        @Override
        public void run() {
            Log.d(TAG, "Start update .............");
            File file = new File(updatePath);
            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);
    }
}
5.下面是三個布局文件

 

sdcardupgrade_warning.xml

 




    

    

        

        

        

        

        

        

        
sdcardupgrade_introduction.xml

 

 




    

    
sdcardupgrade_processbar.xml



    

    

    

    

    

        

        
    
6.字符串資源文件

 

 


    SdCardUpgrade
    Settings
    
    SoftWare Upgrade
    SD card Upgrade
    Warning
    During the upgrade,do not remove the battery,UIM card,and SD card.
    Make sure that the battery level is sufficent.
    You are recommanded to back up data to SD card before upgrade.
    Cancle
    Confirm
    The upgrade takes about two minutes.Do not perform any operations during the upgrade.The upgrade application will automatically stop in 30s.
    Firmware update
    Unpacking...
    The full update will take about 2 minutes.Please do not turn off the device during this period.
    Do not turn off the device.
    Step 1/2
    Step 2/2
    Install...
    Update Complete!
    restart...
    No update package found!
    SD card not mounted!
注意:

 

生成apk後需要使用平台簽名文件進行簽名,或者在Android.mk文件中配置簽名屬性並在源碼中編譯,否則無法運行。

三、最終效果圖

\

\

\

\

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved