Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BroadcastReceiver Vs WakefulBroadcastReceiver

Can somebody explain what the exact difference is between BroadcastReceiver and WakefulBroadcastReceiver?

In what situations would we have to use each Receiver class?

like image 501
user2107111 Avatar asked Oct 15 '14 10:10

user2107111


People also ask

When would you use a Broadcastreceiver?

Broadcast in android is the system-wide events that can occur when the device starts, when a message is received on the device or when incoming calls are received, or when a device goes to airplane mode, etc. Broadcast Receivers are used to respond to these system-wide events.

How do you use WakefulBroadcastReceiver?

WakefulBroadcastReceiver is a helper class that receives a device wakeful event. you shouldoverride onReceive() method where you can call a service or perform your task.

How do I use WakefulBroadcastReceiver on Android?

A WakefulBroadcastReceiver uses the method startWakefulService() to start the service that does the work. This method is comparable to startService() , except that the WakefulBroadcastReceiver is holding a wake lock when the service starts.


1 Answers

There is only one difference between BroadcastReceiver and WakefulBroadcastReceiver.

When you receive the broadcast inside onReceive() method,

Suppose,

BroadcastReceiver :

  • It is not guaranteed that CPU will stay awake if you initiate some long running process. CPU may go immediately back to sleep.

WakefulBroadcastReceiver :

  • It is guaranteed that CPU will stay awake until you fire completeWakefulIntent.

Example:

Here, when you receive broadcast, you are starting a service, as you are using WakefulBroadcastReceiver, it will hold wakelock and won't let the CPU sleep until you finish the work inside service and fire completeWakefulIntent

Code:

public class SimpleWakefulReceiver extends WakefulBroadcastReceiver {     @Override     public void onReceive(Context context, Intent intent) {         // This is the Intent to deliver to our service.         Intent service = new Intent(context, SimpleWakefulService.class);          // Start the service, keeping the device awake while it is launching.         Log.i("SimpleWakefulReceiver", "Starting service @ " + SystemClock.elapsedRealtime());         startWakefulService(context, service);     } }  class SimpleWakefulService extends IntentService {     public SimpleWakefulService() {         super("SimpleWakefulService");     }      @Override     protected void onHandleIntent(Intent intent) {         // At this point SimpleWakefulReceiver is still holding a wake lock         // for us.  We can do whatever we need to here and then tell it that         // it can release the wakelock.  This sample just does some slow work,         // but more complicated implementations could take their own wake         // lock here before releasing the receiver's.         //         // Note that when using this approach you should be aware that if your         // service gets killed and restarted while in the middle of such work         // (so the Intent gets re-delivered to perform the work again), it will         // at that point no longer be holding a wake lock since we are depending         // on SimpleWakefulReceiver to that for us.  If this is a concern, you can         // acquire a separate wake lock here.         for (int i=0; i<5; i++) {             Log.i("SimpleWakefulReceiver", "Running service " + (i+1)                     + "/5 @ " + SystemClock.elapsedRealtime());             try {                 Thread.sleep(5000);             } catch (InterruptedException e) {             }         }         Log.i("SimpleWakefulReceiver", "Completed service @ " + SystemClock.elapsedRealtime());         SimpleWakefulReceiver.completeWakefulIntent(intent);     } } 
like image 150
Mehul Joisar Avatar answered Sep 28 '22 01:09

Mehul Joisar