Android-Broadcast Receiver
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开发者_StackOverflow will be helpful.
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
calledAlarm 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 theActivity
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".
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.
精彩评论