java - 开关只工作一次
问题描述
即使我将 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;
}
谢谢你的帮助!
解决方案
作为记录,答案是将我的 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;
}
推荐阅读
- laravel - Laravel 5.5: Want to run multiple cronjobs through kernel.php under App/Console
- swift - Ipad上的自动布局奇怪行为
- html - 弹性基础:自动在 IE11 中不起作用
- sql - 每次修改表时运行脚本
- node.js - 如何在 React 中使用使用“window”引用的组件与服务器端渲染?
- c# - 使用 Func 和 Expression Func 运行查询之间的差异
- javascript - 如何使用 HTML、CSS、Javascript 在可编辑字段中垂直键入文本...?
- jenkins - 詹金斯的 currentBuild()
- python - PIL 绘制没有灰色轮廓的文本
- intellij-idea - 默认在带有单词边界的路径中查找