我在Android应用中收到以下“Looper类实例计数”错误(在运行KitKat的手机上运行,v4.4.2):
D/ActivityThread: SVC-STOP_SERVICE handled : 0 / android.os.BinderProxy@42137070
D/ActivityThread: SVC-Creating service: CreateServiceData{token=android.os.BinderProxy@41fc59a0 className=com.floor2labs.perp.WakefulMqttService packageName=org.eclipse.paho.android.service.sample intent=null}
D/dalvikvm: create interp thread : stack size=128KB
D/dalvikvm: create new thread
D/dalvikvm: new thread created
D/dalvikvm: update thread list
D/dalvikvm: threadid=15: interp stack at 0x566f9000
D/dalvikvm: threadid=15: created from interp
D/dalvikvm: threadid=15: adding to list
D/dalvikvm: start new thread
D/dalvikvm: threadid=15: notify debugger
D/dalvikvm: threadid=15 (IntentService[WakefulMqttService]): calling run()
E/Looper: WARNING: The Looper class instance count has over a limit(100). There should be some leakage of Looper or HandlerThread.
E/Looper: Looper class instance count = 153
E/Looper: Current Thread Name: IntentService[WakefulMqttService]
I/System.out: java.lang.ThreadGroup[name=main,maxPriority=10]
I/System.out: Thread[main,5,main]
I/System.out: Thread[Thread-5,5,main]
I/System.out: Thread[Binder_1,5,main]
I/System.out: Thread[Binder_2,5,main]
I/System.out: Thread[Thread-1989,5,main]
I/System.out: Thread[Thread-1990,5,main]
I/System.out: Thread[MQTT Rec: a06a1ecf0542,5,main]
I/System.out: Thread[MQTT Snd: a06a1ecf0542,5,main]
I/System.out: Thread[MQTT Call: a06a1ecf0542,5,main]
I/System.out: Thread[Binder_3,5,main]
I/System.out: Thread[IntentService[WakefulMqttService],5,main]
W/System.err: java.lang.Throwable: stack dump
W/System.err: at java.lang.Thread.dumpStack(Thread.java:489)
W/System.err: at android.os.Looper.prepare(Looper.java:104)
W/System.err: at android.os.Looper.prepare(Looper.java:85)
W/System.err: at android.os.HandlerThread.run(HandlerThread.java:54)D/ActivityThread: SVC-CREATE_SERVICE handled : 0 / CreateServiceData{token=android.os.BinderProxy@41fc59a0 className=com.floor2labs.perp.WakefulMqttService packageName=org.eclipse.paho.android.service.sample intent=null}
D/ActivityThread: SVC-Calling onStartCommand: com.floor2labs.perp.WakefulMqttService@41e9e9a8, flags=0, startId=1
D/ActivityThread: SVC-SERVICE_ARGS handled : 0 / ServiceArgsData{token=android.os.BinderProxy@41fc59a0 startId=1 args=Intent { cmp=org.eclipse.paho.android.service.sample/com.floor2labs.perp.WakefulMqttService (has extras) }}
V/INFO: wake-up for mqtt alarm
D/ActivityThread: SVC-Destroying service: com.floor2labs.perp.WakefulMqttService@41e9e9a8
D/ActivityThread: SVC-STOP_SERVICE handled : 0 / android.os.BinderProxy@41fc59a0
D/dalvikvm: threadid=15: exiting
D/dalvikvm: threadid=15: detach (group=0x4190dce0)
D/dalvikvm: threadid=15: removing from list
D/dalvikvm: threadid=15: bye!
错误来自两个类似的服务之一,WakefulMqttService
或WakefulPingService
由WakefulBroadcastReceiver
启动,在警报触发时运行:
警报:
public static void scheduleMqttAlarm(Context context){
AlarmManager alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent nextIntent = new Intent(context, MqttAlarmReceiver.class);
PendingIntent alarmIntent = PendingIntent.getBroadcast(context, 0, nextIntent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmMgr.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 10 * 1000, alarmIntent);
}
上面警报中引用的WakefulBroadcastReceiver:
public class MqttAlarmReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent service = new Intent(context, WakefulMqttService.class);
startWakefulService(context, service);
//schedule the next alarm
MainService.scheduleMqttAlarm(context);
}
}
最后,IntentService从上面开始:
public class WakefulMqttService extends IntentService {
public static Vibrator vibrator;
public WakefulMqttService(String name) {
super(name);
}
public WakefulMqttService() {
super("WakefulMqttService");
}
@Override
protected void onHandleIntent(Intent intent) {
Log.v("INFO", "wake-up for mqtt alarm");
Intent buttonIntent = new Intent("mqttAlarmReceivedEvent");
buttonIntent.putExtra("alarm", "mqtt");
LocalBroadcastManager.getInstance(getBaseContext()).sendBroadcast(buttonIntent);
MqttAlarmReceiver.completeWakefulIntent(intent);
}
}
我相信我已经按照Android开发人员的示例中的所有建议来了解WakefulBroadcastReceiver(链接:WakefulBroadcastReceiver)
但是,似乎没有任何使用这些警报创建的实例正确关闭。几年前发布了一个类似的问题,但它没有得到任何答案。我很感激你解决这个问题的任何帮助。