gpt4 book ai didi

java - 如何在 android 中解决此错误 - java.lang.RuntimeException : Only one Looper may be created per thread

转载 作者:太空狗 更新时间:2023-10-29 15:53:25 25 4
gpt4 key购买 nike

这是我在后台服务中的代码

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

当我运行应用程序时,它会在几秒钟后停止。它在日志控制台中显示以下错误消息请帮我解决这个问题

谢谢

最佳答案

您不能为一个Thread 多次准备Looper。在准备之前,检查 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();
}
};

关于java - 如何在 android 中解决此错误 - java.lang.RuntimeException : Only one Looper may be created per thread,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22296317/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com