您的位置:首页 > 其它

使用 Service 后台处理版本更新 并提示.

2013-03-21 18:02 387 查看

这里版本检测放在了 Service中处理,然后通过注册的 广播 来提示。

public class AppUpdaterService extends Service {
public IBinder onBind(Intent arg0) {
return null;
}
public void onCreate() {
super.onCreate();
new Thread(){
public void run() {
checkVersion(); //后台线程
};
}.start();
}

public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
}
private void checkVersion(){
AppUpdator updator = new AppUpdator(getApplicationContext());
boolean b = updator.checkAppVersion();
if (b) {
System.out.println("Find New Version.");
Intent intent = new Intent();
intent.setAction("NEWVERSION");// NEWVERSION为前面注册的广播ACTION。
sendBroadcast(intent);
stopSelf();
}
}
}


获取清单文件中的版本号,

public static PackageInfo getPackageInfo(Context context) {
String packageName = context.getPackageName();
try {
PackageInfo packageInfo = context.getPackageManager()
.getPackageInfo(
packageName,
PackageManager.GET_CONFIGURATIONS
| PackageManager.GET_ACTIVITIES);
return packageInfo;
} catch (NameNotFoundException e) {
e.printStackTrace();
return null;
}
}


public class AppUpdator {

private Context context;
private String downloadURL = null;
private final String VERSION_URL = "http://192.168.1.110:8080/updater/version.xml";

public AppUpdator(Context context) {
this.context = context;
}

public boolean checkAppVersion() {
float currentVersion = Float.parseFloat(getCurrentVersion());
float latestVersion = Float.parseFloat(getLatestVersion());
if (currentVersion < latestVersion) {
return true;
}
return false;
}

private String getCurrentVersion() {
return MyApp.getPackageInfo(context).versionName;
}

private String getLatestVersion() {
String xml = _readXML();
if (xml != null) {
downloadURL = Utils.substringBetween(xml, "<url>", "</url>"); //这里暂时没用到,直接下载安装时用。
return Utils.substringBetween(xml, "<version>", "</version>");
}
return "1.0";
}

private String _readXML() {
try {
InputStream ins = new HttpClient().connectHTTPGet(VERSION_URL);
if (ins != null) {
BufferedReader br = new BufferedReader(new InputStreamReader(ins));
StringBuffer sb = new StringBuffer();
String line = null;
while ((line = br.readLine()) != null) {
sb.append(line);
}
String xml = sb.toString();
ins.close();
return xml;
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}

}


<?xml version="1.0" encoding="UTF-8"?>

<app>

<version>1.1</version>

<url>http://192.168.1.111/life.apk</url>

</app>

在Activity中启动Service,根据项目要求

startService(new Intent(this,AppUpdaterService.class));


class NewVersionNoticer extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
UIToast.showToastLong("发现新版本.");//这里可以处理发现新版本后的事情,比如直接弹出对话框,可以提供直接下载功能。
}
}


private NewVersionNoticer newVersionNoticer = new NewVersionNoticer();

@Override
protected void onResume() {
super.onResume();
registerReceiver(newVersionNoticer, new IntentFilter("NEWVERSION")); //注册广播,
}


Activity销毁之前 需要调用


protected void onPause() {

super.onPause();

unregisterReceiver(newVersionNoticer);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐