編輯:關於Android編程
二、詳細代碼
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文件中配置簽名屬性並在源碼中編譯,否則無法運行。
三、最終效果圖
從今天起傻蛋打算做一個系列文章,對最新的Android4.0系統中的Launcher,也就是Android4.0原生的桌面程序,進行一個深入淺出的分析,從而引領Andro
Android Activity生命周期以及onSaveInstanceState、onRestoreInstanceState要點備忘 一般的,當
中國移動推出的一卡多號業務可以在已有移動手機號上增加1-3個副號,不用換機、換卡。特別適合想擁有多個手機號碼的用戶或需要保護隱私的用戶服務。在不增加手機、不
越來越多的APP用到了地圖API,所以本人依賴百度地圖提供的API做了一個簡單的項目,希望大家多多支持和star一下!!如果您還沒有去給SoHOT一顆star而直接看這個