首页 > 解决方案 > 开关只工作一次

问题描述

即使我将 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);
            }
        }
    });
}

在服务类中:

    @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;

}

谢谢你的帮助!

标签: javaandroidandroid-servicemenuitem

解决方案


作为记录,答案是将我的 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;

}

推荐阅读