Android发送广播Broadcast
阅读原文时间:2021年04月20日阅读:1

BroadcastReceiver本质上时一种全局的监听器,用于监听系统全局的广播消息,实现系统中不同组件之间的通信。

调用sendBroadcast()即可发送广播,这条广播会启动intent参数所对应的BroadcastReceiver。使用BroadcastReceiver来接受广播。

下面是一个简单的实例

工程结构:

AndroidManifest.xml

<span style="font-size:14px;"><?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.leidong.broadcast">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver android:name=".MyService">
            <intent-filter >
                <action android:name="com.example.leidong.action.MyReceiver"/>
            </intent-filter>
        </receiver>
    </application>

</manifest></span>

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#2b2b2b">

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5pt"
        android:layout_marginRight="5pt"
        android:layout_marginTop="20pt"
        android:text="S E N D"
        android:textColor="#ffffff"
        android:textStyle="bold"
        android:id="@+id/send"
        android:background="#696969"
        android:layout_gravity="center_horizontal" />
</LinearLayout>

MainActivity.java

package com.example.leidong.broadcast;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {
    Button send;

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

        //获取程序界面中的按钮
        send = (Button)findViewById(R.id.send);
        send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //创建Intent
                Intent intent = new Intent();
                intent.setAction("com.example.leidong.action.MyReceiver");
                intent.putExtra("msg", "简单的消息");
                //发送广播
                sendBroadcast(intent);
            }
        });
    }
}

MyReceiver.java

package com.example.leidong.broadcast;

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

/**
 * Created by Lei Dong on 2016/09/05.
 */
public class MyReceiver extends BroadcastReceiver{
    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context,
                "接收到的Intent的Action为:" + intent.getAction() + "\n 消息内容是:" + intent.getStringExtra("msg"),
                Toast.LENGTH_LONG).show();
    }
}

AVD运行效果: