android 應用程序在線版本更新

發布時間:2011年12月26日      浏覽次數:1222 次
第一(yī)步:應用系統配置文件----應用版本配置信息

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="TP.NationalTest"
android:versionCode= "4"
android:versionName= "1.4">
<uses-sdk android:minSdkVersion="4" />
versionCode:版本号
versionName:版本名稱
minSdkVersion:最小(xiǎo)SDk版本

第二步:後台驗證當前應用版本是否要更新
/*檢測應用版本*/
private void CheckAppEdtion()
{
      //判斷網絡連接,如連接
      //Step One 判斷當前是否有網絡連接,
      if(!NetWorkUtility.CheckConnection(this))
      {
            return;
      }
      //獲取網絡平台的版本信息
      CAppInfo model=AppDataServiceHelper.GetAppEdtionInfo(ProjectConstant.AppID);

      //比較版本信息,有新版本 彈出 對話(huà)框
      
      if(model!=null)
      {
             try {
             PackageInfo info = this.getPackageManager().getPackageInfo(this.getPackageName(), 0);
             int oldVersion = info.versionCode;
             int newVersion = Integer.parseInt(model.Edition);
             if(newVersion > oldVersion){
                   //創建更新對話(huà)框,并彈出
                   AppUpdateDialog.CreateRegisterAlert(this);
                   }
            } catch (Exception e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
            }
      }
}

第三步:彈出對話(huà)框 ,用戶選擇是否要更新APK

static ProgressDialog m_Dialog;
static Context m_context=null ;
static AlertDialog appupdatedialog=null;
/*創建查看單題對話(huà)框*/
public static void CreateRegisterAlert(Context context)
{
       m_context=context;
       Dialog dialog = new AlertDialog.Builder(context).setTitle("系統更新")
       .setMessage("已有新版本,是否更新?")
       .setPositiveButton("确定",new DialogInterface.OnClickListener() {
             public void onClick(DialogInterface dialog, int which) {
                   m_Dialog=ProgressDialog.show(m_context, "請等待...", "正在更新,請稍後...",true);
                   mRegsiterHandler.sleep(1000);

             }
       })
       .setNegativeButton("取消", new DialogInterface.OnClickListener() {
             public void onClick(DialogInterface dialog, int which) {
                  
             }
       }).create();
dialog.show();

}

第四步:下(xià)載APK,并更新應用版本
/*
*定時注冊程序
* */
private static RegsiterHandler mRegsiterHandler = new RegsiterHandler();

static class RegsiterHandler extends Handler {

      @Override
      public void handleMessage(Message msg) {
            try{
                  String url="http://xx.xx.xx.xx/test.html";
                  HttpClient client = new DefaultHttpClient();
                  HttpGet get = new HttpGet(url);
                  HttpResponse response;
                  
                   response = client.execute(get);
                   HttpEntity entity = response.getEntity();
                        long length = entity.getContentLength();
                        InputStream is = entity.getContent();
                        FileOutputStream fileOutputStream = null;
                        if (is != null) {

                              File file = new File(Environment.getExternalStorageDirectory(), "test.apk");
                              fileOutputStream = new FileOutputStream(file);

                              byte[] buf = new byte[1024];
                              int ch = -1;
                              while ((ch = is.read(buf)) != -1) {
                                    fileOutputStream.write(buf, 0, ch);
                              }
                        }
                        fileOutputStream.flush();
                        if (fileOutputStream != null) {
                              fileOutputStream.close();
                        }
            }
            catch(Exception ex)
            {}
            finally{
                  m_Dialog.cancel();
            }
            install();
            
      }
      public void sleep(long delayMillis) {
            this.removeMessages(0);
            sendMessageDelayed(obtainMessage(0), delayMillis);
      }
};

private static void install() {
      Intent i = new Intent();
      //i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
      i.setAction(Intent.ACTION_VIEW);
      i.setDataAndType(Uri.fromFile(new File("/sdcard/test.apk")),
                  "application/vnd.android.package-archive");
      m_context.startActivity(i);
      //finish();
}
免責聲明:本站相關技術文章信息部分(fēn)來自網絡,目的主要是傳播更多信息,如果您認爲本站的某些信息侵犯了您的版權,請與我(wǒ)(wǒ)們聯系,我(wǒ)(wǒ)們會即時妥善的處理,謝謝合作!