多次接收相同的意图广播,但只发送一次

Roy*_*ley 6 android broadcastreceiver android-intent

我的意图是在某种条件下发送的.日志证明它仅发送ONCE,但接收器正在接收它多次毫秒.

10-01 10:09:59.201: I/System.out(13543): SENDER CHECKPOINT  
10-01 10:09:59.211: I/System.out(13543): RECEIVER CHECKPOINT 
10-01 10:09:59.291: I/System.out(13543): RECEIVER CHECKPOINT 
Run Code Online (Sandbox Code Playgroud)

我已经确认只有一个广播接收器注册,并且只有一个动作过滤器与注册的BR一起使用.它仅用于单个活动,其中在另一个线程中运行的服务广播intent.同样,日志证实了ONE广播和多个收据.更重要的是,额外信息在回显收据中为空.

怎么会这样?操作系统是否有可能回应它?

发送广播的代码:

private void sendBroadcast(boolean status, String message, String action){
     System.out.println("SENDER CHECKPOINT");

     Intent intent = new Intent(action);
     Bundle bundle = new Bundle();
     bundle.putBoolean("status", status);
     bundle.putString("message", message);
     intent.putExtras(bundle);
     sendBroadcast(intent);

}
Run Code Online (Sandbox Code Playgroud)

接收广播的代码:

private class displayUpdate extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) { 
            System.out.println("RECEIVER CHECKPOINT");


    };
}
Run Code Online (Sandbox Code Playgroud)

注册:

@Override
public void onResume() {
    super.onResume();

    try {
        // just in case onResume is called w/o a pause  
        try{activity.unregisterReceiver(displayReceiver);}catch(Exception e){}

        filterRefreshUI = new IntentFilter(REFRESH_UI);
        activity.registerReceiver(displayReceiver, filterRefreshUI);

    } catch (Exception e) {
        e.printStackTrace();
    }

}
Run Code Online (Sandbox Code Playgroud)

注销:

@Override
public void onPause() {
    super.onPause();
    try{unregisterReceiver(displayReceiver);}catch(Exception e){} 
}
Run Code Online (Sandbox Code Playgroud)

真的很简单!然而接收器发射两次!

如果您想知道 - 在onPause/onResume中处理注册/取消注册,以防止在应用程序被操作系统杀死时泄漏内存.