可能重复:
如何以每 1 分钟的时间间隔获取当前位置?
iam 在工作位置类和另一个 sendms 类上创建
现在我需要在定时器的帮助下创建后台服务,以每 5 分钟后发送短信当前位置
为什么你不能对你的问题做一些研究。为这样一个简单的问题获得解决方案非常简单。
不要多次问同一个问题
无论如何,如果您无法找到它,我可以最好地帮助我的水平
只是您有以下方法可以做到这一点,
通过后台运行Service。
Service注册Location Listener.创建一个alarm, 当Service被调用并设置你的alarm time frequency.
创建一个Broadcast Receiver用于接收您的alarm intent.
unregister您的Location Listener.Service。示例代码将非常冗长,因此您可以轻松获得网络跟踪示例,
ServicealramIntent和registerBroadCast ReceiverBroadCast Receiver和register它与一个alarm Action.实施Location Listener.
Timer与Runnable Thread,创建一个Timer.
ScheduleTimerwithTimerTask run方法。您也可以安排初始启动持续时间和定期间隔呼叫时间延迟。Runnable interface在预定时间发生时调用。Runnable Interface运行UI Thread通过this.runOnUIThread(your_runnable_interface)Runnable run()方法中,注册您LocationListener的更新unregister inonLocationChanged()处理完您的新内容后执行location.receive location再cancel了Timer。这是一个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();
使用 LocationListener 并请求位置更新,详见此处以获取位置更新。我认为您需要一个上下文来执行此操作,但我希望本关于位置更新的教程会有所帮助。
http://www.vogella.com/articles/AndroidLocationAPI/article.html