开关只能工作一次

Switch only works once

即使我已将我的 OnCheckedChangeListener 放在 onCreateOptionsMenu 和 onPrepareOptionsMenu 中,如果我从我的服务中停用该开关,它也只能工作一次。之后再开机就什么都没有了

在 MainActivity 中:

    @Override
public boolean onCreateOptionsMenu(Menu mMenu) {
    getMenuInflater().inflate(R.menu.menu, mMenu);
    switchlistener(mMenu);
    return true;
}

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    mMenu = menu;
    switchlistener(mMenu);
    return super.onPrepareOptionsMenu(menu);
}

方法:

private void switchlistener(Menu mMenu) {
    MenuItem appBarSwitch = mMenu.findItem(R.id.app_bar_switch);
    appBarSwitch.setActionView(R.layout.switch_item);
    mSwitch = mMenu.findItem(R.id.app_bar_switch).getActionView().findViewById(R.id.action_switch);
    mSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
            if (isChecked) {
                Toast.makeText(MainActivity.this, "Fired up!", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(getApplicationContext(), BroadcastReceiverService.class);
                startService(intent);
            } else {
                Intent intent = new Intent(getApplicationContext(), BroadcastReceiverService.class);
                stopService(intent);
            }
        }
    });
}

服役中class:

    @Override
public int onStartCommand(Intent intent, int flags, int startId) {
    if (intent.getAction() != null && intent.getAction().equals("SWITCH OFF")) {
        stopForeground(true);
        stopSelf();

        Menu menu = MainActivity.getThis().getMenu();
        MenuItem appBarSwitch = menu.findItem(R.id.app_bar_switch);
        appBarSwitch.setActionView(R.layout.switch_item);
        appBarSwitch.setChecked(false);

    } else {
        Toast.makeText(this, "Service Started", Toast.LENGTH_SHORT).show();
    }
    return START_NOT_STICKY;

}

感谢您的帮助!

郑重声明,答案是将我的 switchListener 方法放在 OnStartCommand 中。这样,它会再次被调用。

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    if (intent.getAction() != null && intent.getAction().equals("SWITCH OFF")) {
        stopForeground(true);
        stopSelf();

        Menu menu = MainActivity.getThis().getMenu();
        MenuItem appBarSwitch = menu.findItem(R.id.app_bar_switch);
        appBarSwitch.setActionView(R.layout.switch_item);
        appBarSwitch.setChecked(false);
        Switch mSwitch = menu.findItem(R.id.app_bar_switch).getActionView().findViewById(R.id.action_switch);
        mSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                if (isChecked) {
                    Intent intent = new Intent(getApplicationContext(), BroadcastReceiverService.class);
                    startService(intent);
                } else {
                    Intent intent = new Intent(getApplicationContext(), BroadcastReceiverService.class);
                    stopService(intent);
                }
            }
        });


    } else {
        Toast.makeText(this, "Service Started", Toast.LENGTH_SHORT).show();
    }
    return START_NOT_STICKY;

}