开发中,软件的发布更新功能是必不可少的,原来此功能是自己编写的,在一个项目中使用还挺方便,但随着项目的增多,每次需要使用到下载更新功能的时候就又得从原先的项目中去把各种资源整理过来,特别耗时,并且使得项目中的文件资源略多,不利于后期维护。后来在github上去找了一个人家写好的开元工具包,但是在使用中发现有写bug,但总体功能还好,无赖只好咨自寻修改。这里讲组件及使用方式分享出来,供大家参考。
package com.example.mytest;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;
import com.lurencun.service.autoupdate.AppUpdate;
import com.lurencun.service.autoupdate.AppUpdateService;
import com.lurencun.service.autoupdate.ResponseParser;
import com.lurencun.service.autoupdate.Version;
public class TestActivity extends Activity {
// 监测是否更新
private AppUpdate appUpdate;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try {
String UPDATE_URL = "http://home.iusung.com/android/NewMIStxt.txt";
appUpdate = AppUpdateService.getAppUpdate(getApplicationContext());
appUpdate.setCheckUrl(UPDATE_URL);
appUpdate.setIsShowLastVersion(true);
appUpdate.setResponseParser(new ResponseParser() {
@Override
public Version parser(String response) {
// TODO Auto-generated method stub
Version version = null;
try {
JSONObject versionObject = new JSONObject(response);
int versionCode = versionObject.getInt("versionCode");
String versionName = versionObject
.getString("versionName");
String appName = versionObject.getString("releaseApp");
String appName_en = versionObject
.getString("releaseApp_en");
String appDescribe = versionObject
.getString("releaseNote");
String appDescribe_en = versionObject
.getString("releaseNote_en");
String appUrl = versionObject.getString("releaseUrl");
String releaseTime = versionObject
.getString("releaseTime");
version = new Version(versionCode, versionName,
appName, appName_en, appDescribe,
appDescribe_en, appUrl, releaseTime);
} catch (JSONException e) {
e.printStackTrace();
}
return version;
}
});
appUpdate.checkAndShow();
appUpdate.callOnResume();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
Toast.makeText(getApplicationContext(), "检测更新错误,请与管理员联系",
Toast.LENGTH_SHORT).show();
}
}
}