new Timer().schedule(new TimerTask() {
@Override
public void run() {
// TODO Auto-generated method stub
Log.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 block
editor.putString("last" + SysUtil.cur++, message);
editor.commit();
}
}
}, 0, SysUtil.frequncy * 300);
}
目前我是用Timer来实现每隔一段时间执行一次run()里面的程序
但是后来功能需要,发现要动态的改变间隔时间,这一点上timer好像不能实现。
后来改成了while(true)循环里面加Thread.sleep(/*间隔时间*/),try/catch异常
潇潇雨雨
慕田峪9158850
相关分类