Reputation: 1615
here is my code in background service
public class BackgroundService extends Service {
private int interval1 = 60; // 60 seconds
private int interval2 = 60; // 60 seconds
//=============================================================================================================================
private Handler mTimer1 = new Handler();
private Runnable mTask1 = new Runnable() {
public void run() {
Looper.prepare();
Log.w("GPS Tracker", "Tracker going to run "+new Date());
LocationManager mlocManager =(LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocListener = new MyLocationListener();
mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);//, Looper.getMainLooper());
mTimer1.postDelayed(this, interval1 * 1000L);
Looper.loop();
}
};
//=============================================================================================================================
private Handler mTimer2 = new Handler();
private Runnable mTask2 = new Runnable() {
public void run() {
if (isOnline() == true) {
Log.w("Method 2", "setUpdateCardAcceptTag");
setUpdateCardAcceptTag();
Log.w("Method 3", "getCardBulkSerialData");
getCardBulkSerialData();
Log.w("Method 12", "updateStockDataFromRemote");
updateStockDataFromRemote();
Log.w("Method 5", "SetRemarksData");
SetRemarksData();
Log.w("Method 6", "SetCardSaleData");
SetCardSaleData();
Log.w("Method 7", "synchMerchants");
synchMerchants();
Log.w("Method 9", "getUpdatedCities");
getUpdatedCities();
Log.w("Method 10", "getNotifications");
getNotifications();
Log.w("Method 11", "getNextSerialDetails");
getNextSerialDetails();
Log.w("Method 12", "getNextSerialDetails");
//synchLocations();
Log.w("Method 13", "synchLocations");
}
mTimer2.postDelayed(this, interval2 * 1000L);
}
};
when i run the application its stopped after few seconds.its says below error message in log console please help me to sort out this issue
thanks
Upvotes: 2
Views: 1317
Reputation: 1879
Because when a activity or a service(both of them extends ContextWrapper
which extends Context
)is created by android,a Loopper
object will be created also.
In your code ,you use Looper.prepare();
But what does it really do?
public static void prepare() {
prepare(true);
}
private static void prepare(boolean quitAllowed) {
if (sThreadLocal.get() != null) {
throw new RuntimeException("Only one Looper may be created per thread");
}
sThreadLocal.set(new Looper(quitAllowed));
}
(You can find this code in Looper.java)
Because the android os has invoke the mLooper.prepare();
method at the time the service is created,so the sThreadLocal
has a Looper object.When your code is executed,the RuntimeException
will be thrown.
Upvotes: 3
Reputation: 440
Handler runs the 'runnable' in the thread where Handler has been created. You have 2 handlers.. they are in the same thread. I guess they are in main thread .. and main thread already has a looper.. Try to remove "Looper.Prepare" and "Looper.Loop" calls.
Upvotes: 1
Reputation: 11131
You can not prepare the Looper
multiple times for a Thread
. before preparing, Chcek whether Looper
is associated with that Thread
or not.
private Runnable mTask1 = new Runnable() {
public void run() {
if(Looper.myLooper() == null) { // check already Looper is associated or not.
Looper.prepare(); // No Looper is defined So define a new one
}
Log.w("GPS Tracker", "Tracker going to run "+new Date());
LocationManager mlocManager =(LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocListener = new MyLocationListener();
mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);//, Looper.getMainLooper());
mTimer1.postDelayed(this, interval1 * 1000L);
Looper.loop();
}
};
Upvotes: 4