Android广播接收器 [英] Android-Broadcast Receiver

查看:114
本文介绍了Android广播接收器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Android新手.我要知道IntentBroadcastReceiver之间的区别.与Intent相比,我对BroadcastReceiver更加困惑.

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.

推荐答案

好的,我将举一个例子进行说明.

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.

我将拥有:

  • 显示结果的Activity.
  • A Service检查地铁是否正在运行,并在不运行时显示通知.
  • 称为Alarm ReceiverBroadcast Receiver每15分钟调用一次服务.
  • 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);
    }
}

说明: 如您所见,您可以设置一个警报.当收到警报时,我们使用intent启动service.基本上intent是一个味精,可以包含操作和序列化的内容.

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);
    }

}

说明:

service启动并:

  • 设置下一个呼叫的警报. (检查它的意图.只是一个味精)
  • 调用一个AsyncTask,它负责更新通知Activity
  • 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

粘贴AsyncTask没有任何意义,但完成后会调用:

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);
}

这将创建具有特定NEW_SUBWAYS_STATUS动作的新Intent,将其放入地铁和sendBroadcast的意图内.如果有人有兴趣获取该信息,它将有一个收件人.

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:几天前,有人以非常酷的方式解释了广播和意图. 有人想分享他的啤酒,所以他发送了广播 打算采取行动:"FREE_BEER",另外采取行动:一杯啤酒".

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

这篇关于Android广播接收器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆