简体   繁体   English

Android-registerReceiver /多个动作

[英]Android - registerReceiver / multiple action

I have a question about registerReceiver and addAction. 我有一个关于registerReceiver和addAction的问题。

Let's say that I have a servide, which runs in an activity (via a start button). 假设我有一个servide,它在活动中运行(通过“开始”按钮)。 This service will register a receiver via registerReceiver. 该服务将通过registerReceiver注册接收者。 There is something like that in the onStartcommand() method of the service : 服务的onStartcommand()方法中有类似的内容:

final IntentFilter filter1 = new IntentFilter();
filter1.addAction(ACTION_1); // The ACTION is defined in my code
this.br_receiver = new MyBroadcastReceiver();  // The class is defined in my code
this.registerReceiver(this.br_receiver, filter1);
return (START_STICKY);

Now, let's say that I have three different ACTIONS and three checkboxes (one for each ACTION). 现在,假设我有三个不同的ACTIONS和三个复选框(每个ACTION一个)。 When I press the button, it starts the service which checks which ACTIONS it has to add via addAction method(). 当我按下按钮时,它将启动服务,该服务检查必须通过addAction method()添加的操作。 Of course there is a stop button. 当然有一个停止按钮。

My question : when I use the unregisterReceiver() method how can I stop handling only the ACTION I want (I know which ones thanks to the checkbox checked before clicking onto the stop button) ? 我的问题:当我使用unregisterReceiver()方法时,如何停止仅处理所需的操作(由于单击停止按钮之前已选中此复选框,我知道哪些操作)?

I am not sure I am clear so I am going to explain it in a different way : I know that one solution would be to add all the ACTIONS (addAction) when I start the service and then, in the onReceive method(), I could use three boolean in order to know what ACTION to take care of. 我不确定我是否清楚,所以我将以另一种方式进行解释:我知道一种解决方案是在启动服务时添加所有ACTIONS(addAction),然后在onReceive method()中添加可以使用三个布尔值来知道要处理什么ACTION。 I think this solution is crappy. 我认为这种解决方案很糟糕。

Sholuld I use different IntentFilter, one for each ACTION ? 我应该使用不同的IntentFilter,每个ACTION使用一个吗? If yes, how ? 如果是,怎么办?

If you have anwser or even an advice, you're welcome ! 如果您有任何疑问甚至建议,欢迎您!

Thanks. 谢谢。

My question : when I use the unregisterReceiver() method how can I stop handling only the ACTION I want (I know which ones thanks to the checkbox checked before clicking onto the stop button) ? 我的问题:当我使用unregisterReceiver()方法时,如何才能停止仅处理我想要的操作(由于单击停止按钮之前已选中此复选框,我知道哪些操作)?

Option #1: Register three receivers, one per action, and unregister the one for the action that the user unchecks. 选项#1:注册三个接收者,每个动作一个,并为用户取消选中的动作注销一个。

Option #2: Register for only those actions that the user has checked, and when the user changes the checked/unchecked state of a checkbox, unregister and re-register the receiver. 选项#2:仅注册用户已检查的那些动作,并且当用户更改复选框的已选中/未选中状态时,请取消注册并重新注册接收者。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 带有Intent过滤器的Android registerReceiver:是否需要检查操作? - Android registerReceiver with Intent filter: Do I need to check the action? Android:如何在BroadcastReceiver中使用registerReceiver - Android : How to use registerReceiver in BroadcastReceiver registerReceiver BluetoothAdapter.ACTION_STATE_CHANGED不起作用 - registerReceiver BluetoothAdapter.ACTION_STATE_CHANGED not working 对于Android 3的处理,registerReceiver和getApplicationContext函数不存在 - Processing for Android 3, registerReceiver and getApplicationContext functions do not exist Android-如何从其他类中注销registerReceiver - Android - how to unregister the registerReceiver from other class registerReceiver为SCAN_RESULTS_AVAILABLE_ACTION返回null - registerReceiver returns null for SCAN_RESULTS_AVAILABLE_ACTION Android,意图和多项动作 - Android, intent and multiple action Android registerReceiver(null,Intent)获取最新的广播值? - Android registerReceiver(null, Intent) to get lastest broadcasted values? Android registerReceiver等待AlarmManager系统味精的内部运行服务 - Android registerReceiver Inside runing service that waiting for AlarmManager system msg Android多个ACTION_MOVE - Android multiple ACTION_MOVE
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM