Android四大组件之一(broadcast receiver)

认识 Receiver

广播:分为系统广播 与 用户广播

注册:分为静态注册和动态注册

静态注册接收广播

方法:在清单文件 AndroidManifest.xml 里面注册

第一步:创建 CustomReceiver 类 继承 BroadcastReceiver

第二步:静态注册

        <!-- 静态注册广播接受者-->
        <receiver android:name=".CustomReceiver">
            <!--设置过滤标记-->
            <intent-filter >
                <!--这个名字可以随便取 它是广播注册时 与 发送广播时 的 唯一标识,必须要保持一致-->
                <action android:name="com.wust.broadcast"/> 
            </intent-filter>
        </receiver>

并且完善第一步代码

package com.wust.broadcast;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class CustomReceiver extends BroadcastReceiver {
    private static String TAG = CustomReceiver.class.getSimpleName();
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.e(TAG,"CustomReceiver onReceive 广播接受者");
    }
}

第三步:编写布局文件和java代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="静态注册广播,发送广播区域"
        android:textSize="20dp"
        android:textColor="@color/colorAccent"
        android:layout_marginTop="100dp"
        android:layout_gravity="center_horizontal"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="发送广播"
        android:onClick="sendAction2"/>
</LinearLayout>

并编写对应 java 代码

package com.wust.broadcast;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void sendAction2(View view){
        /**
         * Description: 静态发送广播 的 接收者
         * @author: yiqi
         * @date: 2021/4/24 17:17
         * @param:[view]
         * @return:void
         */
        Intent intent = new Intent();
        //这里的 Action 必须与静态注册时保持一致
        intent.setAction("com.wust.broadcast");
        //这一句话是 android 8.0 以上的版本要求写的,因为谷歌鼓励动态注册广播接受者,所以这种方式作为了解,第一个参数是包名;第二个参数是广播接受者的类名
        intent.setComponent(new ComponentName("com.wust.broadcast","com.wust.broadcast.CustomReceiver"));
        sendBroadcast(intent);
    }
}

 动态注册接收广播

注册步骤

 

 第一步:创建 CustomReceiver 类 继承 BroadcastReceiver

package com.wust.broadcast;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class dynamicReceiver extends BroadcastReceiver {
    private static String TAG = dynamicReceiver.class.getSimpleName();
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.e(TAG,"Dynamic Receiver 广播接受者");
    }
}

第二步:java 代码 来 注册 刚刚的接受者即可(动态注册)

package com.wust.broadcast;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ComponentName;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //动态注册
        dynamicReceiver dynamicReceiver = new dynamicReceiver();
        IntentFilter filter = new IntentFilter();
        filter.addAction("dynamicReceiver"); //这个名字可以随便取 它是广播注册时 与 发送广播时 的 唯一标识,必须要保持一致
        registerReceiver(dynamicReceiver,filter);
    }
}

第三步:编写布局文件及java代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="动态注册广播,发送广播区域"
        android:textSize="20dp"
        android:textColor="@color/colorAccent"
        android:layout_marginTop="100dp"
        android:layout_gravity="center_horizontal"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="发送广播"
        android:onClick="sendAction2"/>
</LinearLayout>
package com.wust.broadcast;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ComponentName;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //动态注册
        dynamicReceiver dynamicReceiver = new dynamicReceiver();
        IntentFilter filter = new IntentFilter();
        filter.addAction("dynamicReceiver");
        registerReceiver(dynamicReceiver,filter);
    }
    public void sendAction2(View view){
        /**
         * Description: 动态发送广播 的 接收者
         * @author: yiqi
         * @date: 2021/4/24 17:17
         * @param:[view]
         * @return:void
         */
        Intent intent = new Intent();
        //这里的 Action 必须与动态注册时保持一致
        intent.setAction("dynamicReceiver");
        sendBroadcast(intent);
    }
}

运行效果

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

super码王

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

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

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

打赏作者

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

抵扣说明:

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

余额充值