LocalBroadcastManager未收到广播

时间:2014-03-04 06:01:19

标签: android localbroadcastmanager

我尝试过类似问题的许多不同答案中的例子,但没有任何效果。当我按下后退按钮时,我在日志中收到“广播信息”,但没有收到“广播收到”。为什么呢?

public class MainActivity extends ActionBarActivity 
        implements ContactsFragment.ContactListener {
    private IntentFilter filter;

    private BroadcastReceiver receiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            Log.d(TAG, "Broadcast received");
        }
    };

    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ...

        filter = new IntentFilter(Constants.BROADCAST_ACTION);
        LocalBroadcastManager.getInstance(this).registerReceiver(receiver, filter);

    }

    @Override
    public void onBackPressed() {
        Log.d("sender", "Broadcasting message");
        Intent intent = new Intent(Constants.BROADCAST_ACTION);
        LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
        if (!pane.isOpen()) {
            pane.openPane();
        } else {
            finish();
        }
    }

    @Override
    public void onPause() {
        LocalBroadcastManager.getInstance(this).unregisterReceiver(receiver);
        super.onPause();
    }
}

0 个答案:

没有答案