[英]Switch only works once
Even though I have put my OnCheckedChangeListener in both onCreateOptionsMenu and onPrepareOptionsMenu, the switch only works once if I deactivate it from my service. 即使我将OnCheckedChangeListener放在onCreateOptionsMenu和onPrepareOptionsMenu中,但如果我从服务中将其停用,则该开关仅起作用一次。 After that, nothing happens if I switch it on again.
之后,如果我再次打开它,什么也不会发生。
In MainActivity: 在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);
}
method: 方法:
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);
}
}
});
}
In Service 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;
}
Thanks for any help! 谢谢你的帮助!
For the record, the answer was to put my switchListener method inside the OnStartCommand. 作为记录,答案是将我的switchListener方法放在OnStartCommand中。 That way, it gets called again.
这样,它将再次被调用。
@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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.