As a rule, the registration of the receiver (BroadcastReceiver) is carried out at the time of the creation of the Activity, the registration is canceled when the Activity is deleted. I want to attach this process to a button press, that is: Press the button - the service and receiver start. When the service sends a message, the receiver is disabled. The problem is that each button press registers a new receiver, if the β€œold” is not yet disconnected. To eliminate double (triple, etc.) registration of the receiver, I introduced an additional variable lReceiverOn, the contents of which I check before registration.

public void clickLayout(View view) { startService(new Intent(this, MyServiceFlash.class).putExtra("Mode", 3)); //Запуск сСрвиса, ΠΎΡ‚ΠΊΡƒΠ΄Π° ΠΆΠ΄Π΅ΠΌ сообщСний if (! lReceiverOn) { LocalBroadcastManager.getInstance(this).registerReceiver( mReceiver, new IntentFilter("SrvIntent")); // рСгистрируСм ΠΏΡ€ΠΈΠ΅ΠΌΠ½ΠΈΠΊ сообщСний ΠΈΠ· сСрвиса lReceiverOn = true; // пСрСмСнная контроля запуска ΠΏΡ€ΠΈΠ΅ΠΌΠ½ΠΈΠΊΠ° } ... private BroadcastReceiver mReceiver = new BroadcastReceiver() { // ΠΏΡ€ΠΈΠ΅ΠΌΠ½ΠΈΠΊ сообщСний ΠΈΠ· сСрвиса @Override public void onReceive(Context context, Intent intent) { ... LocalBroadcastManager.getInstance(getApplicationContext()).unregisterReceiver(mReceiver); // ΠΎΡ‚ΠΌΠ΅Π½Π° рСгистрации lReceiverOn=false; } }; 

Question: Is it possible to do without this variable, to determine that the receiver is running? Naively tried (mReceiver! = Null), does not work. Could not find other methods.

  • Describe the task. Judging by the description, you are not inventing something. Why should you turn off the receiver after receiving it? By code: check mRecevier! = Null when clicking. If null initialize it, register it. In onReceive about it. - Yura Ivanov
  • Why do you make the receiver anonymous, bring it to a separate class and you will be happy. Create it in onCreate() and register when pressed - Barmaley
  • The task is as follows: The Service is launched from the Activity, which can be stopped by pressing a button. There is also a button - β€œstop the service after a specified time”. If you press it, the Service starts the timer and in Activity you need to catch the moment when the timer stops. It may, of course, save on matches, but I don’t want to register the receiver when the application starts, as it may not be necessary. Therefore, registration of the receiver occurs only when the timer button is pressed. mRecevier! = null does not work, more precisely, it always gives the "truth" About anonymity ... isn't mRecevier a name? - santfos
  • Your mReceiver is initialized at the place where the class field is declared. Remove from there, initialize it in clickLayout. Shl. Your task does not make sense. In addition, you are trying to add component connectivity, which is avoided when using activity-recevier-service. What will happen if the service stops from another place, or what happens if it is the android itself, and then restart it itself, what if there are not enough resources to start the service? Activity accepts messages, it is not necessary to know the rest. - Yura Ivanov
  • In other words, there are no special methods to determine the registration of the receiver. Either create and modify an additional variable, or reset the BroadcastReceiver variable on its own. Probably no need to bother, just standard to register the receiver when you start Activiti - santfos

0