Track Gps At every 10 minutes using timer in android
Asked Answered
D

2

1

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:

Cannot put code inside thread that has not called looper.prepare().

Does anyone have any insight on this problem?

Diaphoresis answered 7/2, 2011 at 13:36 Comment(0)
A
1

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

this will automatically send update to the listener every 10 min

When ever you got error like Looper . that mean you are not allowed to do something from other then Main UI Worker thread.

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();
Aoudad answered 7/2, 2011 at 13:52 Comment(0)
N
0

This method is easy and can update UI thread:

      new CountdownTimer(30000, 1000) { 

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

    public void onFinish() { 
        mTextField.setText("done!"); 
    } 
 }.start(); 
Nixie answered 7/2, 2011 at 13:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.