Android 四大组件 ————BroadCastReceiver

1,介绍:

【1】在Android中,Broadcast是一种广泛运用的在应用程序之间传输信息的机制。而BroadcastReceiver是对发送出来的 Broadcast进行过滤接受并响应的一类组件。

【2】阐述如何发送Broadcast和使用BroadcastReceiver过滤接收的过程:

    首先在需要发送信息的地方,把要发送的信息和用于过滤的信息(如Action、Category)装入一个Intent对象,然后通过调用 sendOrderBroadcast()或sendStickyBroadcast()方法,把 Intent对象以广播方式发送出去。

  当Intent发送以后,所有已经注册的BroadcastReceiver会检查注册时的IntentFilter是否与发送的Intent相匹配,若匹配则就会调用BroadcastReceiver的onReceive()方法。所以当我们定义一个BroadcastReceiver的时候,都需要实现onReceive()方法。

2,简单使用步骤:

【1】定义一个类继承 BroadcastReceiver 实现里面 onReceive 方法:

import android.app.Notification;

import android.app.NotificationManager;

import android.app.PendingIntent;

import android.content.BroadcastReceiver;

import android.content.Context;

import android.content.Intent;

import android.content.IntentFilter;

public class BroadcastReceiverHelper extends BroadcastReceiver {



    NotificationManager mn=null;

    Notification notification=null;

    Context ct=null;

   BroadcastReceiverHelper receiver;

    public BroadcastReceiverHelper(Context c){

        ct=c;

        receiver=this;

    }

    //注册

    public void registerAction(String action){

        IntentFilter filter=new IntentFilter();

        filter.addAction(action);

        ct.registerReceiver(receiver, filter);

    }

    

    @Override

    public void onReceive(Context context, Intent intent) {

        // TODO Auto-generated method stub

        String msg=intent.getStringExtra("msg");

        int id=intent.getIntExtra("who", 0);

            if(intent.getAction().equals("com.cbin.sendMsg")){

                mn=(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);

                notification=new Notification(R.drawable.icon, id+"发送广播", System.currentTimeMillis());

                Intent it = new Intent(context,Main.class);

                PendingIntent contentIntent=PendingIntent.getActivity(context,

                        0, it, 0);

                notification.setLatestEventInfo(context,

                        "msg", msg, contentIntent);

                mn.notify(0, notification);

            }

    }

}

 

【2】在Android Manifest里面配置receiver主要配置action 。

 注册BroadcastReceiver有两种方式:

  静态注册:在AndroidManifest.xml中用标签生命注册,并在标签内用标签设置过滤器。 

 <receiver android:name="myRecevice">    //继承BroadcastReceiver,重写onReceiver方法

    <intent-filter>    

      <action android:name="com.dragon.net"></action> //使用过滤器,接收指定action广播自己设置

      </intent-filter>

  </receiver> 

  动态注册: 

 IntentFilter intentFilter = new IntentFilter();

  intentFilter.addAction(String);   //为BroadcastReceiver指定action,使之用于接收同action的广播

      registerReceiver(BroadcastReceiver,intentFilter);

  一般:在onStart中注册,onStop中取消unregisterReceiver

  指定广播目标Action:Intent intent = new Intent(actionString);

  并且可通过Intent携带消息 :intent.putExtra("msg", "hi,我通过广播发送消息了");

  发送广播消息:Context.sendBroadcast(intent )

【3】然后再Activity中声明BroadcastReceiver的扩展对象,在onStart中注册,onStop中卸载

BroadcastReceiverHelper  rhelper;

@Override

    public void onStart(){

        //注册广播接收器

        rhelper=new BroadcastReceiverHelper(this);

        rhelper.registerAction("com.cbin.sendMsg");

        super.onStart();

    }

    

    @Override

    public void onStop(){

        //取消广播接收器

        unregisterReceiver(rhelper);

        super.onStop();

    }

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

兴帅_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值