im already looking for the aswer in this forum but none working, im creating an android apps and im trying to get coordinate update from locationlistener in asyncTask (my asyncTask job only to update the latitute and longitude) while the main thread get the coodinate from the asyncTask via Location Manager, and it works yesterday, today its not working anymore, here's my asyncTask code :
private class LoopMsg extends AsyncTask<String,Void,Void>{
protected Void doInBackground(String... urls){
Looper.prepare();
LocationListener listen = new LocationListener() {
@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String arg0) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String arg0) {
// TODO Auto-generated method stub
}
@Override
public void onLocationChanged(Location loc) {
// TODO Auto-generated method stub
Log.d(TAG,"Latitude :"+String.valueOf(loc.getLatitude()));
Log.d(TAG,"Longitude :"+String.valueOf(loc.getLongitude()));
Glatitude = loc.getLatitude();
Glongitude = loc.getLongitude();
}
};
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 60000, 0, listen);
Looper.loop();
return null;
}
}
please help me what went wrong here? and how to fix this? it running good before the next day i got exception at Looper.prepare() it says that only one looper may be created per thread, then i delete Looper.prepare and Looper.loop Part, the error change become
*FATAL EXCEPTION: asyncTask #1
*java.lang.RuntimeException: an error occured while executing do in background
im really don't know why, coz im not update ui thread at my doInBackground method
why i know my app not working : 1.onLocationChanged never called again 2.the app crash if im not using looper.prepare and looper.loop
and I also tried like this :
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 60000, 0, listen, Looper.getMainLooper());
and this :
Looper.Mylooper.prepare();
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 60000, 0, listen, Looper.getMyLooper());
but still no luck,the app still not running
this is the logCat i got after removing the Looper in AsyncTask (and the app always crash)
-Fatal Exception: AsyncTask#2
-java.lang.RuntimeException: An error occured while executing doInBackground()
-at android.os.AsyncTask$3.done(AsyncTask.java:299)
-at java.util.concurrent.FutureTask.finishCompletion(FutureTask.Java:219)
-at java.util.concurrent.FutureTask.run(FutureTask.Java:239)
-at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
-at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
-at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
-at java.lang.Thread.run(Thread.java:838)
-Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
-at android.os.Handler.<init>(Handler.java:197)
-at android.os.Handler.<init>(Handler.java:111)
-at android.location.LocationManager$ListenerTransport$1.<init>(LocationManager.java:199)
-at android.location.LocationManager$ListenerTransport.<init>(LocationManager.java:199)
-at android.location.LocationManager.wrapListener(LocationManager.java:822)
-at android.location.LocationManager.requestLocationUpdates(LocationManager.java:835)
-at android.location.LocationManager.requestLocationUpdates(LocationManager.java:432)
-at android.location.ZTEPrivacyLocationManager.requestLocationUpdates(ZTEPrivacyLocationManager.java:107)
-at com.login.absen.MyService$LoopMsg.doInBackground(MyService.java:244)
-at com.login.absen.MyService$LoopMsg.doInBackground(MyService.java:1)
-at android.os.AsyncTask$2.call(AsyncTask.java:287)
-at java.util.concurrent.FutureTask.run(FutureTask.java:234)
-... 4 more
UPDATE
i guest i can simplyfy the problem Actually the problem was the onLocationChange method not called at (zte n986 - android 4.2) , im trying this code in another device (journey - android 2.3.3) the behavior was the same, asyncTask also stop running at Looper.loop BUT the onLocationChange method was running, so the location update still running