使用BroadcastReceiver发送和接收广播

1 BroadcastReceiver简介

BroadcastReceiver类是所有广播接收器的抽象基类,子类对接收的广播进行筛选并做出响应。广播接收器的生命周期短,当广播消息到达时,调用onReceive()方法,在该方法结束后,BroadcastReceiver实例失效。
发送广播时,通过Activity的sendBroadcast()方法发送广播。
每启动一个广播都需要重新实例化一个新的广播对象,发送的广播分为两类:
(1)普通广播
使用Context.sendBroadcast()发送,异步。广播的接收者以未定义的顺序运行,消息传递的效率较高,缺点是接收者不能将处理结果传递给下一个接收者,且无法终止广播的传播。
(2)有序广播
使用Context.sendOrderedBroadcast()发送,每次只发送给优先级较高的接收者,然后由优先级较高的接收者再传播到优先级较低的接收者。它能完全终止广播。有序接收者顺序由匹配的intent-filter的android:priority属性控制。

2 BroadcastReceiver应用

MainActivity.java

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";


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


        Button button = (Button)findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "onClick >>>");
                Intent intent = new Intent();
                intent.setAction("com.hyh");
                intent.setComponent( new ComponentName( "com.hyh.broadcastreceiver" ,
                        "com.hyh.broadcastreceiver.MyReceiver") );
                sendBroadcast(intent);
            }
        });
    }
}

注意,Android P版本,对于用户发送自定义广播,需要使用setComponent()指定接收广播的包名和类名。
https://blog.csdn.net/qq_30711091/article/details/86657268
MyReceiver.java

public class MyReceiver extends BroadcastReceiver {
    private static final String TAG = "MyReceiver";
    private static final String action = "com.hyh";
    public MyReceiver() {
    }


    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d(TAG,"onReceive >>>");
        if (intent.getAction().equals(action)) {
            Toast.makeText(context, "收到" + action + "广播", Toast.LENGTH_SHORT).show();
        }
    }
}

AndroidManifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.hyh.broadcastreceiver">


    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.BroadcastReceiver">
        <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=".MyReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="com.hyh" />
            </intent-filter>
        </receiver>


    </application>


</manifest>

猜你喜欢

转载自blog.csdn.net/u012906122/article/details/121184364