Developer
Developer

Reputation: 1813

Stop Android BroadCastReceiver

I'm using BroadcastReceiver class in android to get information about network change

using the following code:

 class NetworkStatus extends BroadcastReceiver{    
    ......
    @Override
    public void onReceive(Context context, Intent intent) {
}
       public void startBroadCastReceiver()
{

}

public void  StopBroadCastReceiver()
{
}

I want to stop the broadcast receiver and then start it again How can I do this

Upvotes: 0

Views: 173

Answers (2)

TieDad
TieDad

Reputation: 9899

Broadcast receiver likes event handler, Android system calls it automatically when a broadcast matches you defined intent. If you define the boardcast receiver in manifast file, I think the only way you can do it to return immediately when you don't want to handle the broadcast, like the following:

public void onReceive(Context context, Intent intent) {
     if (stoppedBroadcast)
          return;

     // handle broadcast.
}

public void stopBroadcast {
     stoppedBroadcast = true;
}

public void resumeBroadcast {
     stoppedBroadcast = false;
}

Upvotes: 1

Cattivo
Cattivo

Reputation: 752

If you want to stop recieving network changes try unregisterReciever in your registering activity/service. When you want to enable recieving register the reciever with its IntentFilter again.

Upvotes: 0

Related Questions