这是我在后台服务中的代码
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);
}
};
当我运行应用程序时,它在几秒钟后停止。在日志控制台中显示以下错误消息 请帮我解决这个问题
感谢
答案 0 :(得分:4)
您无法为Looper
多次准备Thread
。在准备之前,请检查Looper
是否与Thread
相关联。
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();
}
};
答案 1 :(得分:3)
因为当一个活动或服务(它们都扩展ContextWrapper
扩展Context
)由android创建时,也会创建一个Loopper
对象。
在您的代码中,您使用Looper.prepare();
但它真正做到了什么?
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));
}
(你可以在Looper.java中找到这段代码)
因为android os在创建服务时调用了mLooper.prepare();
方法,所以sThreadLocal
有一个Looper对象。当你的代码执行时,RuntimeException
将是抛出。
答案 2 :(得分:1)
Handler在创建Handler的线程中运行'runnable'。你有2个处理程序..它们在同一个线程中。我猜他们在主线程..并且主线程已经有一个looper ..尝试删除“Looper.Prepare”和“Looper.Loop”调用。