Track Gps At every 10 minutes using timer in android
I am developing an androi开发者_如何学运维d 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?
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();
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();
精彩评论