0

可能重复:
如何以每 1 分钟的时间间隔获取当前位置?

iam 在工作位置类和另一个 sendms 类上创建

现在我需要在定时器的帮助下创建后台服务,以每 5 分钟后发送短信当前位置

4

2 回答 2

4

为什么你不能对你的问题做一些研究。为这样一个简单的问题获得解决方案非常简单。

不要多次问同一个问题

每5分钟后如何获取位置?.

无论如何,如果您无法找到它,我可以最好地帮助我的水平

只是您有以下方法可以做到这一点,

  1. 通过后台运行Service

    • 创建一个Service注册Location Listener.
    • 创建一个alarm, 当Service被调用并设置你的alarm time frequency.

    • 创建一个Broadcast Receiver用于接收您的alarm intent.

    • 获取位置后,unregister您的Location Listener.
    • 当你觉得,没有必要去取位置然后停下来Service

示例代码将非常冗长,因此您可以轻松获得网络跟踪示例,

  • 创建一个Service
  • 创建一个alramIntentregisterBroadCast Receiver
  • 创建一个BroadCast Receiverregister它与一个alarm Action.
  • 实施Location Listener.

    1. 通过TimerRunnable Thread,
  • 创建一个Timer.

  • ScheduleTimerwithTimerTask run方法。您也可以安排初始启动持续时间和定期间隔呼叫时间延迟。
  • Runnable interface在预定时间发生时调用。
  • 确保Runnable Interface运行UI Thread通过this.runOnUIThread(your_runnable_interface)
  • Runnable run()方法中,注册您LocationListener的更新
  • 然后做unregister inonLocationChanged()处理完您的新内容后执行location.
  • 当你觉得,没必要receive locationcancelTimer

这是一个Timer使用示例,

//Declared Globally in class
Timer timer;
//Timer follows here

timer = new Timer();
timer.schedule(new TimerTask() {            
@Override
public void run() {
callRunnableMethod();
}
},Set your initial start time, Set your time delay for interval);



//callRunnableMethod() goes here


private void callRunnableMethod()
{
//This method is called directly by the timer
//and runs in the same thread as the timer.
//We call the method that will work with the UI
//through the runOnUiThread method.

context or this .runOnUiThread(Location_Runnable);
}


//Location_Runnable goes here

private Runnable Location_Runnable = new Runnable() {
public void run() {
//This method runs in the same thread as the UI.               
//Do something to the UI thread here

Just start calling or request for updates with your location listener here.
}
};


//Once you decided to stop the periodic location fetching then do cancel your timer,

timer.cancel();
于 2013-02-01T07:53:22.980 回答
0

使用 LocationListener 并请求位置更新,详见此处以获取位置更新。我认为您需要一个上下文来执行此操作,但我希望本关于位置更新的教程会有所帮助。

http://www.vogella.com/articles/AndroidLocationAPI/article.html

于 2013-02-01T05:48:40.380 回答