使得应用可通过后台自动检查是否有最新版本,如果需要更新当前版本,将弹出对话框让用户选择是否在当前通过Market来更新软件。
Knowledge Points:
SharedPreferences: 一个轻量级的存储方法,类似于经常使用的.ini文件,它也是通过检索关键字来取得相应的数值。之所以是成为轻量级,是因为它所能应用的数值类型有限,对于存储较大数值,效率相对较低。
System.currentTimeMillis:将当前时间以毫秒作为单位来表示,用于比较两个时间的先后顺序。(其数值表示从1970-01-01 00:00:00直到当前时间的总毫秒数)
通过网络来读取信息:在checkUpdate()方法中包含了通过制定的URL来读取网络资源。具体操作步骤,请参考源代码。
Runnable: 在其内部的Run()方法中实现所要执行的任何代码,当这个runnable interface被调用后可以视作为新的线程。
public class Test extends Activity {
private Handler mHandler;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.front);
mHandler = new Handler();
/* Get Last Update Time from Preferences */
SharedPreferences prefs = getPreferences(0);
lastUpdateTime = prefs.getLong("lastUpdateTime", System.currentTimeMillis());
/* Should Activity Check for Updates Now? */
if ((lastUpdateTime + (24 * 60 * 60 * 1000)) < System.currentTimeMillis()) {
/* Save current timestamp for next Check*/
lastUpdateTime = System.currentTimeMillis();
SharedPreferences.Editor editor = getPreferences(0).edit();
editor.putLong("lastUpdateTime", lastUpdateTime);
editor.commit();
/* Start Update */
checkUpdate.start();
}
}
/* This Thread checks for Updates in the Background */
private Thread checkUpdate = new Thread() {
public void run() {
try {
URL updateURL = new URL("http://my.company.com/update");
URLConnection conn = updateURL.openConnection();
InputStream is = conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
ByteArrayBuffer baf = new ByteArrayBuffer(50);
int current = 0;
while((current = bis.read()) != -1){
baf.append((byte)current);
}
/* Convert the Bytes read to a String. */
final String s = new String(baf.toByteArray());
/* Get current Version Number */
int curVersion = getPackageManager().getPackageInfo("your.app.id", 0).versionCode;
int newVersion = Integer.valueOf(s);
/* Is a higher version than the current already out? */
if (newVersion > curVersion) {
/* Post a Handler for the UI to pick up and open the Dialog */
mHandler.post(showUpdate);
}
} catch (Exception e) {
}
}
};
/* This Runnable creates a Dialog and asks the user to open the Market */
private Runnable showUpdate = new Runnable(){
public void run(){
new AlertDialog.Builder(Test.this)
.setIcon(R.drawable.icon)
.setTitle("Update Available")
.setMessage("An update for is available!nnOpen Android Market and see the details?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
/* User clicked OK so do some stuff */
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://search?q=pname:your.app.id"));
startActivity(intent);
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
/* User clicked Cancel */
}
})
.show();
}
};
}
分为三个部分:
1.置于onCreate()方法中的程序用于判断当前时间是否需要检查更新(如果距离上次更新时间大于1天,将启动检查更新)
2.当以上条件满足时,启动checkUpdate来检查当前程序是否为最新版本。
3.如果确定版本已过期,那么将登录market,并直接指向当前程序页面。
分享到:
相关推荐
WPF实现客户端版本的检测,更新操作,实现客户端程序的自动更新。
android是当今移动互联网最受欢迎的一门编程语言,互联网移动产品愈来愈多,版本更新成为一个必不可少的环节,本资源提供一个jar包来读取apk的配置文件来检测版本更新。
版本检测功能实现 当发布在应用市场的上的应用升级后,可根据版本号 检测更新 本方法是根据 解析xml文件进行更新 可换成webservice接口处理
App版本管理,包括版本检测和强制更新
ios版本检查更新,不影响上线
易语言利用QQ空间检测版本更新源码,利用QQ空间检测版本更新,检测版本,文本__取中间_后面取起,交互回调函数
android 检查服务器更新版本,与服务器的版本号对比当前版本号,不同,就提示有新版本,点击下载就自动下载更新。
整理的一个PHP检测APP版本是否有更新的方法,下载后请按自己需求修改
自动检测版本更新及自动安装
一般android app应用程序会隔一段时间发布一个新的版本,包括新增功能和修复原程序bug,当你打开某个app,如果检测到有最新的版本,会提醒你是否下载更新。
Android项目版本检测自动更新.rar
android 自动检测版本和更新下载
app版本检测更新,实现从service下载,ProgressBar显示下载进度。
可以实现强制用户更新是一个实用程序,检查用户的当前安装的版本iOS应用程序对应用商店中当前可用的版本。如果一个新版本可用,UIAlertView的实例提出了新版本的用户通知他们,并给予他们更新应用程序的选项。
版本检测自动更新
安卓Android源码——版本检测自动更新.zip
介绍: ... * 检查版本更新 */ - (void)checkVerionUpdate { QJCheckVersionUpdate *update = [[QJCheckVersionUpdate alloc] init]; [update showAlertView]; }
Andorid项目源码 版本检测自动更新完整版