(android實戰)應用線上版本更新

來源:互聯網
上載者:User

Step One 應用系統設定檔----應用版本配置資訊

<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:最小SDk版本

Step Two 後台驗證當前應用版本是否要更新

 

  /*檢測應用版本*/
private void CheckAppEdtion()
{
//判斷網路連接,如串連
//Step One 判斷當前是否有網路連接,
if(!NetWorkUtility.CheckConnection(this))
{
return;
}
//擷取網路平台的版本資訊
CAppInfo model=AppDataServiceHelper.GetAppEdtionInfo(ProjectConstant.AppID);

//比較版本資訊,有新版本 彈出 對話方塊

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){
//建立更新對話方塊,並彈出
AppUpdateDialog.CreateRegisterAlert(this);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}


Step THree 彈出對話方塊 ,使用者選擇是否要更新APK

  static ProgressDialog  m_Dialog;
static Context m_context=null ;
static AlertDialog appupdatedialog=null;
/*建立查看單題對話方塊*/
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();

}

 Step four 下載APK,並更新應用版本

View Code

   /*
*定時註冊程式
* */
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/gointel/nationaltest.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(), "nationaltest.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/nationaltest.apk")),
"application/vnd.android.package-archive");
m_context.startActivity(i);
//finish();
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.