我注册了一个广播监听器,并在app的一个service开的一个子线程里面循环发送广播,然而这个广播接收器一开始能执行onReceive,过了几分钟以后,就不执行onReceive了,我调试了很久,确定广播已经发出去了,并且服务没有被杀死,并且广播没有被解除注册,并且我尝试了静态和动态注册,后来我甚至开了多个广播,然而到某个时间点,这些广播同时不起作用了,是怎么回事?
//这是其中一个BroadcastReceiver
public class TestReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
System.out.println("testReceiver: " + intent.getStringExtra("msg"));
}
}
//这是发送广播的部分
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
while(true){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
long time=System.currentTimeMillis();//long now = android.os.SystemClock.uptimeMillis();
SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d1=new Date(time);
String t1=format.format(d1);
Intent intent = new Intent("android.intent.action.BLE_BROADCAST");
intent.putExtra("msg", t1);
sendBroadcast(intent);
intent = new Intent("com.android.action.test");
intent.putExtra("msg", t1);
sendBroadcast(intent);
System.out.println("已发送广播");
}
}
}).start();
}