Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android-Broadcast Receiver

Tags:

android

I am new to android. I what to know the difference between Intent and BroadcastReceiver. I am more confused with BroadcastReceiver than Intent.

Please help me out. Simple code will be helpful.

like image 728
bharathi Avatar asked Aug 30 '10 09:08

bharathi


People also ask

What is the broadcast receiver in Android?

A broadcast receiver (receiver) is an Android component which allows you to register for system or application events. All registered receivers for an event are notified by the Android runtime once this event happens.

What are the types of broadcast receivers in Android?

There are two types of broadcast receivers: Static receivers, which you register in the Android manifest file. Dynamic receivers, which you register using a context.

What is difference between service and broadcast receiver in Android?

A Service receives intents that were sent specifically to your application, just like an Activity. A Broadcast Receiver receives intents that were broadcast system-wide to all apps installed on the device.

What is the limit of broadcast receiver in Android?

As a general rule, broadcast receivers are allowed to run for up to 10 seconds before they system will consider them non-responsive and ANR the app.


2 Answers

Ok, I will explain it with an example.

Let's suppose I want to create an app to check subway status from it's webpage. I also want a system notification if the subway is not working ok.

I will have:

  • An Activity to show results.
  • A Service to check if the subway is working and show a notification if it's not working.
  • A Broadcast Receiver called Alarm Receiver to call the service every 15 minutes.

Let me show you some code:

/* AlarmReceiver.java */
public class AlarmReceiver extends BroadcastReceiver {
    public static final String ACTION_REFRESH_SUBWAY_ALARM =
          "com.x.ACTION_REFRESH_SUBWAY_ALARM";

    @Override
    public void onReceive(Context context, Intent intent) {
        Intent startIntent = new Intent(context, StatusService.class);
        context.startService(startIntent);
    }
}

Explanation: As you can see you can set an alarm. and when the alarm is received we use an intent to start the service. Basically the intent is a msg which can have actions, an serialized stuff.

public class StatusService extends Service {

    @Override
    public void onCreate() {
        super.onCreate();
        mAlarms = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        Intent intentToFire = new Intent(AlarmReceiver.ACTION_REFRESH_ALARM);
        mAlarmIntent = PendingIntent.getBroadcast(this, 0, intentToFire, 0);
    }

    @Override
    public void onStart(Intent intent, int arg1) {
        super.onStart(intent, arg1);
        Log.d(TAG, "SERVICE STARTED");
        setAlarm();
        Log.d(TAG, "Performing update!");
        new SubwayAsyncTask().execute();
        stopSelf();
    }

    private void setAlarm() {
        int alarmType = AlarmManager.ELAPSED_REALTIME_WAKEUP;
        mAlarms.setInexactRepeating(alarmType, SystemClock.elapsedRealtime() + timeToRefresh(),
                    AlarmManager.INTERVAL_HALF_DAY, mAlarmIntent);
    }

}

Explanation:

The service starts and:

  • Set the alarm for the next call. (Check the intent it's used. Just a msg)
  • Calls an AsyncTask which takes care of updating an notifying the Activity

It doesn't make sense to paste the AsyncTask but when it finished it calls:

private void sendSubwayUpdates(LinkedList<Subway> subways) {
      Intent intent = new Intent(NEW_SUBWAYS_STATUS);
      intent.putExtra("subways", subways);

      sendBroadcast(intent);
}

This creates a new Intent with a certain NEW_SUBWAYS_STATUS action, put inside the intent the subways and sendBroadcast. If someone is interested in getting that info, it will have a receiver.

I hope I made myself clear.

PS: Some days ago someone explained broadcast and intents in a very cool way. Someone wants to share his beer, so he sends a broadcast with an intent having action:"FREE_BEER" and with an extra: "A glass of beer".

like image 109
Macarse Avatar answered Oct 04 '22 02:10

Macarse


The API states:

A BroadcastReceiver is a base class for code that will receive intents sent by sendBroadcast().

An intent is an abstract description of an operation to be performed.

So, a BroadcastReceiver is just an Activity that responds to Intents. You can send your own broadcasts or even the Android Device can send these system wide broadcasts including things like the battery is low, or the device just booted-up.

like image 40
user817129 Avatar answered Oct 04 '22 00:10

user817129