BroadcastReceiver不会从ActionBarDrawerToggle触发

时间:2017-10-31 14:50:23

标签: android android-fragments broadcastreceiver navigation-drawer

我尝试通知我的片段抽屉是打开还是关闭,以便停止更新UI。我决定发送一个广播,抽屉开始打开和关闭。

但是,片段中的接收器永远不会被触发。我不明白为什么,因为我多次使用广播。

主要活动:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close) {
            @Override
            public void onDrawerStateChanged(int newState) {
                super.onDrawerStateChanged(newState);
                if (newState == DrawerLayout.STATE_SETTLING) {

                    if (!drawer.isDrawerOpen(GravityCompat.START)) {
                        // Start opening
                        Log.d(TAG, "start opening");
                        Intent i = new Intent();
                        i.setAction(getResources().getString(R.string.DrawerAction));
                        i.putExtra(getResources().getString(R.string.drawer_extra), true);
                        getApplicationContext().sendBroadcast(i);
                    }
                    else {
                        // start closing
                        Log.d(TAG, "start closing");
                        Intent i = new Intent();
                        i.setAction(getResources().getString(R.string.DrawerAction));
                        i.putExtra(getResources().getString(R.string.drawer_extra), false);
                        getApplicationContext().sendBroadcast(i);
                    }
                }
            }
        };
        drawer.setDrawerListener(toggle);
        toggle.syncState(); 

片段:

private class DrawerReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            Log.d(TAG, "onreceiver drawer");
            if (intent != null) {
                Log.d(TAG, "intetn not null");
                if (intent.getAction().equals(context.getResources().getString(R.string.DrawerAction))) {
                    Log.d(TAG, "drawer action ok");
                    boolean isOpen = intent.getBooleanExtra(context.getResources().getString(R.string.drawer_extra), false);
                    Log.d(TAG, "isdraweropen in watchfrag : "+isOpen);
                    // If drawer open, stop update UI, stop recording
                    if (isOpen) {
                        updateUI = false;
                    } else {
                        updateUI = true;
                    }
                }
            }
        }
    }

@Override
    public void onStart() {
        super.onStart();
        getActivity().registerReceiver(drawerReceiver, new IntentFilter(getResources().getString(R.string.drawer_filter)));

    }

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

        getActivity().unregisterReceiver(drawerReceiver);
    }


 @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        drawerReceiver = new DrawerReceiver();
    }

谢谢大家。

1 个答案:

答案 0 :(得分:1)

您在IntentFilter上使用的操作似乎是注册接收方:

new IntentFilter(getResources().getString(R.string.drawer_filter))

与您正在广播的Intent上的操作不匹配:

i.setAction(getResources().getString(R.string.DrawerAction))

那些动作String必须相同才能使接收者获得这些广播。

此外,您可以考虑使用LocalBroadcastManager(在v4支持库中提供)进行应用本地广播。它使用相同的IntentBroadcastReceiver设置,但它比在Context上发送广播要快得多。如果您决定走这条路线,请务必注册接收方并在LocalBroadcastManager发送广播。

当然,任何其他事件总线实现也可以在这里工作,但如果你没有在你的应用程序中使用此模式进行其他任何操作,则可能会有些过分。 LocalBroadcastManager非常轻量级,因为它使用框架中已有的组件。