android:实现定时任务
我正在制作一个应用程序,如果应用程序打开,我必须在 1 小时后发送 GPS 协调,并且一次在注销后打开应用程序意味着当应用程序启动时,GPS 协调将发送到服务器,并且如果应用程序保持打开状态1小时内,将发送gps坐标,如果应用程序在1小时前关闭,则应用程序将不会发送gps坐标。我的代码如下:
TimerTask timer_task = new TimerTask() {
public void run()
{
Log.v(".............................................", "Timer Task Started");
System.out.println("@@@@@@@@@@ timer task started IN TRACKER11111");
try
{
// if (locn != null)
{
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
String time = sdf.format(cal.getTime());
String xml = xml parameters being send to server
System.out.println(xml);
System.out.println("hello");
System.out.println("Xml is : "+xml);
// FileSave obj9=new FileSave();
// obj9.Save(xml);
int len = xml.length();
byte[] data = xml.getBytes();
System.out.println("Length =**************************** " + len);
System.out.println("Stream Closed");
conMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
if (conMgr.getActiveNetworkInfo() != null && conMgr.getActiveNetworkInfo().isAvailable() && conMgr.getActiveNetworkInfo().isConnected())
{
//if (gps.equals("yes"))
{
new Connection(data);
}
}
else
{
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
};
Timer timer = new Timer();
timer.scheduleAtFixedRate(timer_task, 4000, 3600000);
}
catch (Exception e1) {
e1.printStackTrace();
}
I am making an app in which i have to send gps coordinated after 1 hour if the app is open and one time when app is opened after logging out means when the app starts the gps coordinated will be send to server and if the app remained opened for 1 hour , gps coordinates will be send and if the app gets closed before 1 hour , the app will not send gps coordinates.My code is as follows:
TimerTask timer_task = new TimerTask() {
public void run()
{
Log.v(".............................................", "Timer Task Started");
System.out.println("@@@@@@@@@@ timer task started IN TRACKER11111");
try
{
// if (locn != null)
{
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
String time = sdf.format(cal.getTime());
String xml = xml parameters being send to server
System.out.println(xml);
System.out.println("hello");
System.out.println("Xml is : "+xml);
// FileSave obj9=new FileSave();
// obj9.Save(xml);
int len = xml.length();
byte[] data = xml.getBytes();
System.out.println("Length =**************************** " + len);
System.out.println("Stream Closed");
conMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
if (conMgr.getActiveNetworkInfo() != null && conMgr.getActiveNetworkInfo().isAvailable() && conMgr.getActiveNetworkInfo().isConnected())
{
//if (gps.equals("yes"))
{
new Connection(data);
}
}
else
{
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
};
Timer timer = new Timer();
timer.scheduleAtFixedRate(timer_task, 4000, 3600000);
}
catch (Exception e1) {
e1.printStackTrace();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需相应地更改给定的代码:
并在位置管理器中设置时间和最小距离,如下所示:
其中 3600000 是时隙,40is 距离(以米为单位),mloc 管理器和 mloc 侦听器是位置管理器和位置侦听器
Just change the given code accordingly:
and set the time and mininum distance in location manager which is as follows:
where 3600000 is the time slot and 40is distance in metres and mloc manager and mloc listener are location manager and location listener