自定义广播实例

  我们应该已经学会了通过广播接收者来接收系统广播的内容,但是在实际开发中,仍需要自定义一些广播。下面我们就来讲解如何在应用程序中发送自定义的广播。

  发送广播很简单,只需要声明一个意图,然后使用Context.sendBroadcast()方法发送意图即可。这里在布局文件中加入一个 Button 按钮来触发发送广播的事件,activity_main.xml代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
 <Button
        android:id="@+id/send"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="发送广播"
        android:textSize="30dp"/>
</LinearLayout>

在Activity中对按钮的点击事件进行处理,发送广播。MainActivity代码如下:

package com.rfstar.normalbroadcasttest;
import androidx.appcompat.app.AppCompatActivity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.send).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //建立一个意图,action为com.rfstar.action.NORMAL_BROADCAST
                Intent intent=new Intent("com.rfstar.action.NORMAL_BROADCAST");
               // 这一行在Android 7.0及如下版本不是必须的,可是Android 8.0或者更高版本,发送广播的条件更加严苛,必须添加这一行内容。
               // 建立的ComponentName实例化对象有两个参数,第1个参数是指接收广播类的包名,第2个参数是指接收广播类的完整类名。
                intent.setComponent(new ComponentName("com.rfstar.normalbroadcasttest","com.rfstar.normalbroadcasttest.MyReceiver"));
                Bundle bundle=new Bundle();
                bundle.putString("name","大鸟科创空间");
                bundle.putString("field","科技");
                //向意向中加入数据
                intent.putExtra("data",bundle);
                //发送广播,普通广播
                sendBroadcast(intent);

            }
        });
    }
}

这里的代码很简单,只是在按钮的点击事件里面加入了发送自定义广播的逻辑。首先构建出一个Intent 对象,并使用Bundle存储要传递的值,并用 intent.putExtra("data",bundle)把要发送的值传入,然后调用了 Context 的 sendBroadcast()方法将广播发送出去,这样所有监听 com.rfstar.action.NORMAL_BROADCAST这条广播的广播接收者就会收到消息。普通广播被发送之后,还需要定义一个广播接收者来准备接收此广播。这里新建一个 MyReceiver 类继承自 BroadcastReceiver 类,代码如下∶

package com.rfstar.normalbroadcasttest;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

public class MyReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        //获取广播的action
        String action=intent.getAction();
        Bundle bundle=intent.getBundleExtra("data");
        String name=(String)bundle.get("name");
        String field=(String)bundle.get("field");
        Toast.makeText(context,"接收到自定义的普通广播,其中action为:"+action+
                ";接收的广播数据为,名称:"+name+",领域:"+field,Toast.LENGTH_LONG).show();
    }
}

逻辑很简单,当 MyReceiver 收到自定义的广播时获取相关的数据,并弹出"接收到自定义的普通广播"和接收到的数据。除了上述代码外,还需要在 AndroidManifest.xml 中对这个广播接收者进行注册∶

<receiver android:name=".MyReceiver">
    <intent-filter>
        <action    android:name="com.rfstar.action.NORMAL_BROADCAST">
        </action>
    </intent-filter>
</receiver>

<intent-filter>标签中 action 的 name 值正是在 Activity 类中声明的Intent 的 action,两者需要保持一致。运行程序并点击按钮,此时将发出一条普通广播,如下图所示。这样就成功完成了发送自定义普通广播的功能。

源码下载地址:链接:https://pan.baidu.com/s/1ExoSxaQRdjmwUUnDuSbUIQ 提取码:43ka

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值