使用Android中的计时器每10分钟跟踪一次Gps [英] Track Gps At every 10 minutes using timer in android
问题描述
我正在开发一个android应用程序,并且正在使用一项服务,该服务每10分钟通过GPS跟踪一次位置.但是,当我将位置侦听器放入计时器任务中时,它会引发异常:
I am developing an android application and I am using a service that tracks location via GPS every 10 minutes. But when I put the location listener in a timer task, it throws an exception:
无法将代码放入未调用
looper.prepare()
的线程中.
有人对此问题有见识吗?
Does anyone have any insight on this problem?
推荐答案
您无需启动服务即可接收位置每10分钟更新一次 而是做这样的事情
You need not to start a service for recieving locationUpdate every 10 min instead do something like this
mLocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
mLocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 600000, 0, mLocListener);
这将每10分钟自动将更新发送给侦听器
this will automatically send update to the listener every 10 min
每当出现Looper之类的错误时.这意味着除了Main UI Worker线程之外,您不能做其他事情.
When ever you got error like Looper . that mean you are not allowed to do something from other then Main UI Worker thread.
您正在运行什么代码并收到此错误.从主UI线程执行代码.或者 通过主线程在您的主类中创建一个Handler对象
What so code you are running and getting this error . execute the code from Main UI Thread. or create a Handler object in your main class by Main Thread
Handler mHandler = new Handler();
然后在其他任何线程中执行类似的操作
Then in any of your other thread do something like this
new Thread(new Runnable(){ public void run(){
// any of your code
mHandler.post(new Runnable(){public void run(){
// the code giving you error Looper
}});
}}).start();
这篇关于使用Android中的计时器每10分钟跟踪一次Gps的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!