Android应用的定时更新

Android应用的定时更新,本人使用的是Service+Broadcast+AlarmManager.

具体如下:

在Service的onCreate方法中定义一个广播接收器。然后使用AlarmManger定时,在到达指定时间后则发送广播。在广播接收器中检查版本。

如果当前版本大于等于服务器版本,则不需要下载,重新定时。如果小于,则启动下载。


代码如下:

public class HomeService extends Service{
	public static final String TAG = "HomeService";
	/**用于定时的任务*/
	PendingIntent pendingIntent;
	public static final String DOWN_ACTION_NAME = "com.mypay.down";
	/** 小时,用于定时*/
	public static final int HOUR = 14;
	/** 分钟,用于定时*/
	public static final int MINUTE = 17;
	
	
	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public void onCreate() {
		// 注册下载广播
		IntentFilter iFilter = new IntentFilter();
		iFilter.addAction(DOWN_ACTION_NAME);
		registerReceiver(mReceiver, iFilter);
		Log.i("updateService", "onCreate");	
		
		super.onCreate();
	}
	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		setDownBroadcast();
		return super.onStartCommand(intent, flags, startId);
	}
	/**
	 * 设置一个定时广播<p/>
	 * 到达指定时间之后发送广播
	 */
	private void setDownBroadcast() {
		//定时下载广播,时间到达之后会发送DOWN_ACTION_NAME的广播
		AlarmManager aManager = (AlarmManager)getSystemService(ALARM_SERVICE);
		Intent downBroadCast = new Intent(DOWN_ACTION_NAME);
		pendingIntent = PendingIntent.getBroadcast(HomeService.this, 0,
				downBroadCast, PendingIntent.FLAG_CANCEL_CURRENT);
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.HOUR_OF_DAY, HOUR);
		calendar.set(Calendar.MINUTE, MINUTE);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		if (calendar.getTimeInMillis()<System.currentTimeMillis()) {
			//定时时间 小于当前时间,则定时时间改为第二天
			calendar.add(Calendar.DAY_OF_MONTH, 1);
			aManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
		}
		aManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
	}
	
	/**定义一个广播接收器,用于控制定时任务*/
	public BroadcastReceiver mReceiver = new BroadcastReceiver() {
		
		
		AlarmManager alarmManager;
		@Override
		public void onReceive(Context context, Intent intent) {
			Log.i("BroadcastReceiver", "onReceive");
			//先取消定时任务
			alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
			alarmManager.cancel(pendingIntent);
			
			
			//执行异步任务
			new GetLastVersion().execute();		
		}
		/**
		 * 异步任务,获取最新版本
		 * @author WangZk
		 * 2013年11月29日
		 */
		class GetLastVersion extends AsyncTask<String, String, AppVersion>{

			@Override
			protected AppVersion doInBackground(String... params) {
				//获取新版本
				
				return null;
			}
			protected void onPostExecute(AppVersion result) {
				//执行成功
				handler.sendEmptyMessage(1);
			}
		}
		public Handler handler = new Handler(){
			public void handleMessage(android.os.Message msg) {
				Log.i("handler", "异步任务--获取最新版本,执行完成");
				if (msg.what==1) {
					
				    if (判断条件)
					{
						//服务器版本大于本地版本,则启动DownLoadService去下载并安装最新版本
						Intent downloadIntent = new Intent();
						downloadIntent.setClass(HomeService.this, DownLoadService.class);
						startService(downloadIntent);
						//测试用:设置多久更新一次,可以注销
//						Calendar calendar = Calendar.getInstance();
//						alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis()+3000, pendingIntent);
					}else{
						//如果小于,则继续定时,此时时间为第二天
						Calendar calendar = Calendar.getInstance();
//						calendar.setTimeInMillis(System.currentTimeMillis());
//						calendar.clear();日历是唯一实例,clear之后会使清空Calendar
						calendar.set(Calendar.HOUR_OF_DAY, HOUR);
						calendar.set(Calendar.MINUTE, MINUTE);
						calendar.set(Calendar.SECOND, 0);
						calendar.set(Calendar.MILLISECOND, 0);
						//此时的时间肯定会大于上一次检查时间
						calendar.add(Calendar.DAY_OF_MONTH, 1);
						alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
						
					}
				}
			};
		};
}


以上代码有所更改,是从工程中截取出来的,用汉字注明了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值