Android-Broadcast Receiver

前端 未结 2 466
暖寄归人
暖寄归人 2020-12-29 07:48

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

2条回答
  •  情歌与酒
    2020-12-29 08:33

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

提交回复
热议问题