二、详细代码
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文件中配置签名属性并在源码中编译,否则无法运行。
三、最终效果图