跟踪Gps每10分钟使用android中的计时器

时间:2011-02-07 13:36:18

标签: android android-service android-gps android-looper

我正在开发一个Android应用程序,我正在使用每10分钟通过GPS跟踪位置的服务。但是当我将位置监听器放入计时器任务时,它会引发异常:

  

无法将代码放入未调用looper.prepare()的线程中。

有没有人对此问题有任何见解?

2 个答案:

答案 0 :(得分:1)

您无需每10分钟启动一次接收locationUpdate的服务 而是做这样的事情

 mLocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
 mLocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 600000, 0, mLocListener);

这将每10分钟自动向监听器发送更新

当你遇到像Looper这样的错误时。这意味着你不允许从其他主UI工作者线程做一些事情。

您运行的代码是什么,并收到此错误。从主UI线程执行代码。要么  通过主线程在主类中创建一个Handler对象

 Handler mHandler = new Handler();

然后在你的任何其他线程中做这样的事情

  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();

答案 1 :(得分:0)

此方法很简单,可以更新UI线程:

      new CountdownTimer(30000, 1000) { 

    public void onTick(long millisUntilFinished) { 
        mTextField.setText("seconds remaining: " + millisUntilFinished / 1000); 
    } 

    public void onFinish() { 
        mTextField.setText("done!"); 
    } 
 }.start();