我知道这是一个老问题,但为什么不使用广播接收器来获取一个意图,然后触发所有接收器取消注册?(希望发布比当前答案更准确的内容)
在响应的片段/活动中,你把它放在:
public class PanicFragment extends Fragment {
IntentFilter killFilter = new IntentFilter("your.app.name.some.awesome.action.title");
BroadcastReceiver kill = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
context.unregisterReceiver(receiver); // The actual receiver you want to unreigster
context.unregisterReceiver(this); // The one you just created
}
};
Run Code Online (Sandbox Code Playgroud)
(在创建片段/活动时,不要忘记最初注册接收者)
在您的服务或其他活动中或您想要的任何内容:
private void callThisToUnregisterAllYourReceivers(Context context) {
Intent killThemAll = new Intent();
killThemAll.setAction("your.app.name.some.awesome.action.title");
context.sendBroadcast(killThemAll);
}
Run Code Online (Sandbox Code Playgroud)
我希望这在任何方面都有帮助
| 归档时间: |
|
| 查看次数: |
4277 次 |
| 最近记录: |