定时器Timer实时修改时间间隔(周期)

上传人:ji****72 文档编号:37670909 上传时间:2018-04-20 格式:DOCX 页数:6 大小:17.42KB
返回 下载 相关 举报
定时器Timer实时修改时间间隔(周期)_第1页
第1页 / 共6页
定时器Timer实时修改时间间隔(周期)_第2页
第2页 / 共6页
定时器Timer实时修改时间间隔(周期)_第3页
第3页 / 共6页
定时器Timer实时修改时间间隔(周期)_第4页
第4页 / 共6页
定时器Timer实时修改时间间隔(周期)_第5页
第5页 / 共6页
点击查看更多>>
资源描述

《定时器Timer实时修改时间间隔(周期)》由会员分享,可在线阅读,更多相关《定时器Timer实时修改时间间隔(周期)(6页珍藏版)》请在金锄头文库上搜索。

1、1. 定时器 Timer 当时间间隔(周期)变化时,实时去修改我项目中主要是实现在某个时间段上午8:00-12:00,下午2:00(pm_start)-19:00(pm_end),每隔3分钟(pm_time)(可以手动设置)自动上传当前的位置(google的定位)/ 执行定时任务private boolean bool = true;/ true表示服务器与本地数据相同 ,false表示服务器与本地数据不相同private void start_schedule_pm() final Timer timer = new Timer();timerTask = new TimerTask() O

2、verridepublic void run() /当不相同时取消timer重新new 一个timerif (!bool) if (timer != null) Log.e(“%“, bool + “);timerTask.cancel();timer.cancel();start_schedule_pm();bool = true;return;if (getCurrentTime() - 12) = Double.valueOf(pm_end) timerTask.cancel();timer.cancel();/我的是在android service里跑的程序,所以关闭了services

3、topSelf();return;if (!AppUtils.checkNet(getApplicationContext() Log.d(“data“, “网络未连接“);return;Log.d(“data“, “新任务开始“);if (location != null) if (getCurrentTime() - 12) Double.valueOf(pm_start) /这里需要handler来实现,否则报Cant create handler inside thread that has not called Looper.prepare()mHandler.sendEmptyMe

4、ssage(1);mHandler.sendEmptyMessage(2);timer.schedule(timerTask, 0, Long.valueOf(pm_time) * 60 * 1000);Handler mHandler = new Handler() public void handleMessage(Message msg) super.handleMessage(msg);switch (msg.what) case 1:/google定位服务onLocationChanged(location);/ getSqlData();/获取服务器数据并与本地数据相比较,如果不相

5、同update本地数据库,bool=falsegetSetTime();break;case 2:locationManager.requestLocationUpdates(provider, 30 * 1000, 0,LocationService.this);break;default:break;/google定位服务即获取当前的location需要implements LocationListenerlocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);Criteria criteria = ne

6、w Criteria();provider = locationManager.getBestProvider(criteria, true);location = locationManager.getLastKnownLocation(provider);public void onLocationChanged(Location location) / 获得当前位置的纬度latitude = location.getLatitude();/ 获得当前位置的经度longitude = location.getLongitude();Log.e(“经纬度分别为:“, Calendar.getInstance().getTime() + “longitude=“+ longitude + “latitude“ + latitude);/ 获取当时的时间(24小时制)SimpleDateFormat sDateFormat = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss“);time = sDateFormat.format(new Date();/把数据上传到服务器isPositionAvailable();

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 行业资料 > 其它行业文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号