In my application I need to receive an activation code from SMS when user registered. For that I have a BroadcastReceiver
but I don't want in to always enabled and I need it just once when user is registering. How to do that?
Asked
Active
Viewed 88 times
1

Amir_P
- 8,322
- 5
- 43
- 92
-
You can use local broadcast receiver register in single class. Or you can put condition to check on receiver. You can refer http://stackoverflow.com/questions/18842517/broadcast-receiver-class-and-registerreceiver-method/18842810#18842810 – Bhoomika Brahmbhatt Apr 18 '16 at 12:16
2 Answers
2
You can call registerReceiver and unregisterReceiver in your activity. Here is an example of it :
registerReceiver(receiver, filter);
unregisterReceiver(receiver);
where receiver is your broadcast receiver class and filter will be
private IntentFilter filter = new IntentFilter("android.provider.Telephony.SMS_RECEIVED");
incase of sms receiver.
You can call registerReceiver once you require to receive a sms and call unregisterReceiver to stop listening for incoming sms.

nilkash
- 7,408
- 32
- 99
- 176
-
but without registering it is working. i want it to be disabled untill i register it – Amir_P Apr 18 '16 at 12:34
-
-
-
Don't register your broadcast receiver in AndroidManifest file. Register it run time. I mean call registerReceiver once you require it . – nilkash Apr 18 '16 at 12:38
0
For this you need to register the Broadcast Receiver in the activity; it will remain alive only for that instance of activity. Instead of registering it in manifest just register in the activity on runtime when required.

Aashutosh Sharma
- 1,483
- 2
- 17
- 29
-
how to register receiver without defining it in manifest? please post code – Amir_P Apr 18 '16 at 12:33
-
you can check this answer for reference http://stackoverflow.com/questions/7439041/how-to-unregister-broadcastreceiver – Aashutosh Sharma Apr 18 '16 at 12:36
-
You can check this tutorial to understand both ways: http://www.vogella.com/tutorials/AndroidBroadcastReceiver/article.html – Aashutosh Sharma Apr 18 '16 at 12:38