1

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?

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 Answers2

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
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