Android——自定义广播之发送求救广播案例

自定义广播介绍

当系统提供的广播不能满足实际需求时,可以自定义广播。

当自定义广播发送消息时,会将消息存储到公共消息区中,
而公共消息区中如果存在对应的广播接收者,则会及时接受这条信息。

一、创建程序ForHelp

指定包名为cn.itcast.forhelp

二、布局activity_main.xml文件

这里只放了一个按钮

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".MainActivity">

    <Button
        android:id="@+id/btn_help"
        android:text="发送求救广播"
        android:layout_centerHorizontal="true"
        android:padding="5dp"
        android:layout_marginTop="50dp"
        android:textSize="20sp"
        android:background="#ffd2d2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>

三、创建广播接收者

选中程序中的 cn.itcast.forhelp包,右击,【new】-【Java Class】创建一个MyBroadcastReceiver并继承BroadcastReceiver,用于接收发送的广播信息。

若创建后代码下有红色曲线,按住Alt+enter,生成方法就好了。

这里重写了onReceive()方法,在该方法中通过Log打印接收到的广播信息。

package cn.itcast.forhelp;

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

public class MyBroadcastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i("MyBroadcastReceiver","自定义的广播接收者,接收到了求救广播事件");
        Log.i("MyBroadcastReceiver",intent.getAction());
    }
}

四、编写MainActivity页面交互代码

1.创建一个init()方法,在该方法中获取发送求救广播的控件并实现该控件的点击事件。
通过registerReceiver()方法动态注册MyBroadcastReceiver广播接收者。

        receiver = new MyBroadcastReceiver();//实例化广播接收者
        String action = "Help_Stitch";
        IntentFilter intentFilter = new IntentFilter(action);
        registerReceiver(receiver, intentFilter);//动态注册广播

通过findViewById()方法获取控件btn_help,并实现该按钮的点击事件。在该事件中,通过Intent对象的setAction()方法设置广播的action名称,该名称必须与动态注册的广播接收者的action名称一致,否则接收不到发送的广播信息,然后通过sendBroadcast()方法发送广播。

Button btn_help = findViewById(R.id.btn_help);
        btn_help.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();//定义广播的事件类型
                intent.setAction("Help_Stitch");
                sendBroadcast(intent);//发送广播
            }
        });

2.重写onDestroy()方法,在该方法中通过unregisterReceiver()方法注销广播接收者MyBroadcastReceiver

3.具体代码如下:

package cn.itcast.forhelp;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
private MyBroadcastReceiver receiver;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
    }
    private void init() {
        receiver = new MyBroadcastReceiver();//实例化广播接收者
        String action = "Help_Stitch";
        IntentFilter intentFilter = new IntentFilter(action);
        registerReceiver(receiver, intentFilter);//动态注册广播
        Button btn_help = findViewById(R.id.btn_help);
        btn_help.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();//定义广播的事件类型
                intent.setAction("Help_Stitch");
                sendBroadcast(intent);//发送广播
            }
        });
    }
     protected void  onDestroy(){
            super.onDestroy();
            unregisterReceiver(receiver);
    }

}

五、运行程序

点击“发送求救广播”按钮,程序会发送一条广播信息。此时可以看到logcat窗口中打印的信息。

 

猜你喜欢

转载自blog.csdn.net/weixin_72634509/article/details/127986955