Android——BroadcastReceiver

注释:一般广播不会被阻断,有序广播则会被阻断

注释:这是用动态注册的广播,必须要解绑

xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.chenshuai.myapplication.Activityreceiver"
    android:orientation="vertical">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="发送一般广播"
        android:textSize="20sp"
        android:onClick="yibanguangboonclick"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="发送有序广播"
        android:textSize="20sp"
        android:onClick="youxuguangboonclick"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="动态注册广播接收器"
        android:textSize="20sp"
        android:onClick="dongtaizconclick"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="解注册广播接收器"
        android:textSize="20sp"
        android:onClick="jiezconclick"/>


</LinearLayout>
Activityreceiver.java
package com.example.chenshuai.myapplication;

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

public class Activityreceiver extends AppCompatActivity {

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

    //发送一般广播
    public void yibanguangboonclick(View view)
    {
        //发送一般广播
        //1.准备Intent
        Intent intent = new Intent("com.example.chenshuai.action");

        intent.putExtra("data","广播发送了,你要注意了");

        //2.发送
        sendBroadcast(intent);

        Toast.makeText(Activityreceiver.this, "我发送了广播", Toast.LENGTH_SHORT).show();

    }
    //发送有序广播
    public void youxuguangboonclick(View view)
    {
        //发送有序广播
        //1.准备Intent
        Intent intent = new Intent("com.example.chenshuai.action");

        intent.putExtra("data","有序广播发送了,你要注意了");

        //2.发送
        sendOrderedBroadcast(intent, null);

        Toast.makeText(Activityreceiver.this, "我发送了有序广播", Toast.LENGTH_SHORT).show();

    }

    MyReceiver_receiver2 myReceiver_receiver2;
    public void dongtaizconclick(View view)
    {
        //动态注册
        //1- 实例化接收器
        if (myReceiver_receiver2 == null) {
            myReceiver_receiver2 = new MyReceiver_receiver2();

            //2- 实例化IntentFilter
            IntentFilter intentFilter = new IntentFilter("com.example.chenshuai.action");
            intentFilter.setPriority(1000);
            //3- 注册
            registerReceiver(myReceiver_receiver2, intentFilter);
        }
    }

    //解动态注册
    public void jiezconclick(View view)
    {
        if (myReceiver_receiver2 != null)
        {
            unregisterReceiver(myReceiver_receiver2);

            myReceiver_receiver2 = null;

            Toast.makeText(Activityreceiver.this, "解注册接收器", Toast.LENGTH_SHORT).show();
        }
    }

    //防止用户忘记解注册
    @Override
    protected void onDestroy() {

        super.onDestroy();
        if (myReceiver_receiver2 != null)
        {
            unregisterReceiver(myReceiver_receiver2);

        }
    }
}
MyReceiver_receiver.java
package com.example.chenshuai.myapplication;

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

public class MyReceiver_receiver extends BroadcastReceiver {
    public MyReceiver_receiver() {

        Log.e("TAG","构造广播接收器");
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO: This method is called when the BroadcastReceiver is receiving
        // an Intent broadcast.
        //throw new UnsupportedOperationException("Not yet implemented");

        String str = intent.getStringExtra("data");

        Log.e("TAG","收到广播了"+str);
    }
}
MyReceiver_receiver2.java
package com.example.chenshuai.myapplication;

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

public class MyReceiver_receiver2 extends BroadcastReceiver {
    public MyReceiver_receiver2() {

        Log.e("TAG","构造广播接收器2");
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO: This method is called when the BroadcastReceiver is receiving
        // an Intent broadcast.
        //throw new UnsupportedOperationException("Not yet implemented");

        String str = intent.getStringExtra("data");

        //处理广播
        Log.e("TAG","收到广播了2"+str);

        Toast.makeText(context, "我阻断了有序广播", Toast.LENGTH_SHORT).show();

        //判断是否是有序广播
        if (isOrderedBroadcast())
        {
            abortBroadcast();

            Log.e("TAG","我阻断了广播");
        }
    }
}

minifest.xml

 <receiver
            android:name=".MyReceiver_receiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter android:priority="100">
                <action android:name="com.example.chenshuai.action"/>
            </intent-filter>
            </receiver>
      <!--  <receiver
        android:name=".MyReceiver_receiver2"
        android:enabled="true"
        android:exported="true">
        <intent-filter android:priority="1000">
            <action android:name="com.example.chenshuai.action"/>
        </intent-filter>
    </receiver>-->

测试静态广播时在里面注册,测试动态广播时不需要。

原文地址:https://www.cnblogs.com/Chenshuai7/p/5429748.html