new Timer().schedule(new TimerTask() {@Overridepublic void run() {// TODO Auto-generated method stubLog.i("aaa", "hello");TelephonyManager tm = (TelephonyManager)getBaseContext().getSystemService(Context.TELEPHONY_SERVICE);deviceid = tm.getDeviceId();username = SysUtil.loginname;SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd");SimpleDateFormat stimeFormat = new SimpleDateFormat("HH:mm:ss");date = sDateFormat.format(new java.util.Date());time = stimeFormat.format(new java.util.Date());//发送message = username + ","+ deviceid + ","+ date + ","+ time + ","+ preferences.getString("longtitude", "fail") + ","+ preferences.getString("latitude", "fail") + ","+ preferences.getString("accelerator", "fail,fail,fail") + ","+ preferences.getString("magnetic", "fail,fail,fail") + ","+ preferences.getString("pressure", "0") + ","+ preferences.getString("light", "0") + ","+ preferences.getString("wifi", "fail,fail,fail");try {String s = SysUtil.client.sendMessage(message);if(s.equals("fail")){editor.putString("last" + SysUtil.cur++, message);editor.commit();}if(s.equals("login")){SysUtil.pushlogin(getApplicationContext());editor.putString("last" + SysUtil.cur++, message);editor.commit();}if(s.equals("success")){SysUtil.cur--;for(; SysUtil.cur > 0; SysUtil.cur--){preferences.getString("last" + SysUtil.cur, message);s = SysUtil.client.sendMessage(message);if(!s.equals("success"))break;}}} catch (Exception e) {// TODO Auto-generated catch blockeditor.putString("last" + SysUtil.cur++, message);editor.commit();}}}, 0, SysUtil.frequncy * 300);}目前我是用Timer来实现每隔一段时间执行一次run()里面的程序但是后来功能需要,发现要动态的改变间隔时间,这一点上timer好像不能实现。后来改成了while(true)循环里面加Thread.sleep(/*间隔时间*/),try/catch异常
2 回答
潇潇雨雨
TA贡献1833条经验 获得超4个赞
我觉得吧
你暂停的时候应该吧TIMER.CANCEL()掉,也就是退出TIMER,等恢复的时候再重新调用TIMER
如果时间是变动的,那么你就不要用TIMER,自己NEW 一个THREAD然后在THREAD里设置一个标志变量,可以再外面访问的,用它来控制线程的退出,里面再调用this.sleep(),
不能再主线程里直接SLEEP,那样程序会全停掉
添加回答
举报
0/150
提交
取消