Android广播的发送与接收

今天写了一下安卓广播的发送与接收,其中遇到了不少的问题,记录一下

首先,先随便创建一个Activity,布局文件如下:(两个按钮分别是动态注册和静态注册方法)

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity18">

    <Button
        android:id="@+id/button35"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="静态注册"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/button33" />

    <Button
        android:id="@+id/button33"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="动态注册"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

接下来我们先写动态注册方法,先写一个BootCompleteReceiver类要继承BroadcastReceiver,重写onReceiver方法

package com.example.myapplication;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class BootCompleteReceiver extends BroadcastReceiver {
    public BootCompleteReceiver(){

    }

    @Override
    public void onReceive(Context context, Intent intent) {//下面的语句根据自己的需求来写
        String name=intent.getStringExtra("name");
        Toast.makeText(context, "广播的内容是:"+name, Toast.LENGTH_SHORT).show();
    }
}

 接下来到创建好的Activity中去让按钮发送广播

private IntentFilter intentFilter;
private BootCompleteReceiver bootCompleteReceiver;


public class MainActivity18 extends AppCompatActivity {
    private IntentFilter intentFilter;
    private BootCompleteReceiver bootCompleteReceiver;
    



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main17);
        Button button33=findViewById(R.id.button33);
        intentFilter=new IntentFilter("MY_ACTION");//括号的的action值可以自定义,也可以用广播的action,只要让它与下面的intent中的值一致
        bootCompleteReceiver=new BootCompleteReceiver();
        registerReceiver(bootCompleteReceiver,intentFilter);
        button33.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent();
                intent.setAction("MY_ACTION");
                intent.putExtra("name","奥特曼");//传递给广播接收器
                sendBroadcast(intent);
            }
        });

注册完要记得关闭

@Override
    protected void onDestroy() {
        unregisterReceiver(bootCompleteReceiver);
        super.onDestroy();
    }

然后是静态注册,有的人可能没反应,我知道的错误一个是AndroidManifest.xml中没写对或者没添加权限,还有一个就是比较新的版本,静态广播要指定一个范围

下面是在AndroidManifest.xml中要添加的内容

<receiver android:name=".MyBroadcastReceiver"
                  android:enabled="true"
                  android:exported="true">
            <intent-filter android:priority="100">//类似于优先级之类的东西
                <action android:name="com.example.broadcasts.MY_BROADCAST"/>
            </intent-filter>
        </receiver>

<!-- 下面这个权限并不是我程序中所要用到的,我只是拿他举个例子,
就是说如果你的广播要监听什么,实现什么功能,有些功能是需要添加权限的,
不添加这个广播就可能没用  -->
<permission android:name=“android.permission.WRITE_SETTINGS”/>//具体想要什么权限可以去自己搜

下面定义一个MyBroadcastReceiver类,要继承BroadcastReceiver,这个类作用同上

package com.example.myapplication;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class MyBroadcastReceiver extends BroadcastReceiver {
    public MyBroadcastReceiver(){

    }

    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "我是奥特曼", Toast.LENGTH_SHORT).show();
        abortBroadcast();
    }
}

接下来只要在原来的Activity里添加如下语句就可以了,记得加上intent.setPackage(getPackageName());否则新版本安卓广播可能用不了

Button button35=findViewById(R.id.button35);
        button35.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent("com.example.broadcasts.MY_BROADCAST");//其中的action依然是我自定义的
                intent.setPackage(getPackageName());//最好加上这句话
                sendBroadcast(intent);
            }
        });

看一下运行结果

点击动态注册

点击静态注册

 

 大概就是这么多啦,有不对的地方请大家多多纠正,谢谢大家。

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值