广播机制1111111111

最后效果
在这里插入图片描述

在这里插入图片描述
前台代码:activity_main.xml

<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"
    android:gravity="center">

    <TextView
        android:id="@+id/smsStatusTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="等待发送广播..."
        android:textSize="20sp"
        android:layout_marginBottom="20dp"/>
    <Button
        android:id="@+id/sendBroadcastButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="发送广播"/>

</LinearLayout>



后台
MainActivity

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private static final String CUSTOM_SMS_RECEIVED_ACTION =
            "com.example.myapplication.ACTION_NEW_SMS_RECEIVED";
    private MyCustomReceiver customReceiver; // 自定义Receiver
    private IntentFilter intentFilter;
    private TextView smsStatusTextView;
    private Button sendBroadcastButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main); // 布局文件

        smsStatusTextView = findViewById(R.id.smsStatusTextView); // TextView控件ID
        sendBroadcastButton = findViewById(R.id.sendBroadcastButton); // 按钮ID

// --- 动态注册广播 ---
        customReceiver = new MyCustomReceiver();

// 设置UI更新回调(通过Receiver的接口)
        customReceiver.setOnUIUpdateListener(message ->
                smsStatusTextView.setText(message)
        );

        intentFilter = new IntentFilter();
        intentFilter.addAction(CUSTOM_SMS_RECEIVED_ACTION); // 添加监听Action

        registerReceiver(customReceiver, intentFilter); // 注册广播
// --- 注册结束 ---

// --- 按钮点击发送广播 ---
        sendBroadcastButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent customBroadcastIntent = new Intent(
                        CUSTOM_SMS_RECEIVED_ACTION
                );
                sendBroadcast(customBroadcastIntent); // 发送普通广播
            }
        });
// --- 监听器结束 ---
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(customReceiver); // 注销广播
        customReceiver = null;
    }
}

创建类
MyCustomReceiver.java

package com.example.myapplication;

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

public class MyCustomReceiver extends BroadcastReceiver {
    private static final String TAG = "MyCustomReceiver";
    private OnUIUpdateListener uiUpdateListener;

    // 定义UI更新回调接口
    public interface OnUIUpdateListener {
        void updateStatus(String message);
    }

    // 设置回调的方法
    public void setOnUIUpdateListener(OnUIUpdateListener listener) {
        this.uiUpdateListener = listener;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d(TAG, "Received broadcast: " + intent.getAction());

        String CUSTOM_ACTION = "com.example.myapplication.ACTION_NEW_SMS_RECEIVED";

        // 正确获取Action并匹配
        if (CUSTOM_ACTION.equals(intent.getAction())) {
            // 通过回调更新UI(而非直接操作TextView)
            if (uiUpdateListener != null) {
                uiUpdateListener.updateStatus("收到新的短信息了。 (通过自定义广播)");
                Log.d(TAG, "UI update callback triggered.");
            } else {
                Log.e(TAG, "UI update listener is null.");
            }
        }
    }
}

使用自定义广播机制完成广播接收者的定义,实例化,注册。点击“发送广播”按钮后,发送一条自定义广播,模拟接收到短信的广播,然后接收者在收到广播后,在主界面显示“收到新的短信息了…”。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值