使用 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);
}
相关文章推荐
- 网站后台登录aspcms 提示错误号:-2147467259,错误描述:操作必须使用一个可更新的查询。sql=update AspCms_Content set TimeStatus=0 where TimeStatus=1 and Timeing <= 解决方法。
- 出现“无法通过windows installer服务安装此安装程序包。您必须安装带有更新版本Windows Installer服务的Windows Service Pack”提示如何解决?
- 判断ie浏览器版本是否过低,然后提示更新或者使用其他浏览器
- phpcms v9后台登陆模板修改方法和程序版本更新提示修改方法
- 使用google play services lib时 提示需要更新版本
- Android APP更新——service后台下载、进度提示、自动安装
- CoreLocation 的基本使用 以及定位 指南针的实现 (附加: 系统版本适配的方法,和后台更新用户位置的方法及注意)
- Android使用service后台更新计划任务
- 百度编辑器UEEDITOR使用简单介绍 UEditor表单提交和后台交互详解 最后更新对应的版本:1.2.5.1 教程描述: 富文本编辑器的使用开发中,表单提交有多种场景,编辑器初始化有新增文章和编辑
- windows server 2003中使用cocos2d-x,提示OpenGL版本为1.1,要更新驱动,解决方法
- MySQL二进制包使用mysql_upgrade版本更新升级MySQL 5.7
- Android中使用IntentService创建后台服务实例
- Android提示版本更新
- 《连载 | 物联网框架ServerSuperIO教程》- 16.集成OPC Server,及使用步骤。附:3.3 发布与版本更新说明。
- $.getJSON() 方法第二个参数的使用以及后台servlet如何处理。
- vs2010 ocx项目中使用ffmpeg release版本注册提示缺少DLL
- 提示用户进行版本更新并且发布通知监控版本下载情况
- 使用CocoaPods时提示安装高版本ruby遇到的坑。
- jQuery上传插件Uploadify 3.2使用 - 后台处理代码
- 关于“操作必须使用一个可更新的查询”错误提示!