Android-动态注册广播和注销广播

静态注册广播

广播机制在安卓开发很常见也很重要,它既可以静态注册,也可以 
动态注册和注销,广播可以设置优先级。 
首先是静态 
activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical"
    >
    <Button
        android:id="@+id/btnSend"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="发送广播"
        />
</LinearLayout>

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

设置一个按钮用于发送广播 
创建一个广播类AgainMyReceiver.java继承BroadcastReceiver 
AgainMyReceiver.java

package com.xieth.as.againbroadcastreceiver;

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

public class AgainMyReceiver extends BroadcastReceiver {
    public AgainMyReceiver() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {

        Log.d("AgainMyReceiver", "静态广播接收到了消息");

    }
}

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

打印日志便于测试查看,然后在AndroidManifest.xml修改

  <receiver
            android:name=".AgainMyReceiver"
            android:enabled="true"
            android:exported="true" >
  </receiver>
 
 
  • 1
  • 2
  • 3
  • 4
  • 5

然后在MainActivity.java定义事件 
MainActivity.java

package com.xieth.as.againbroadcastreceiver;

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

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Intent it = null;

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

        findViewById(R.id.btnSend).setOnClickListener(this);

        it=  new Intent(MainActivity.this, AgainMyReceiver.class);

    }

    @Override
    public void onClick(View v) {

        int id = v.getId();
        switch (id) {
            case R.id.btnSend:
                sendBroadcast(it);
                break;
            default:
                break;
        }

    }
}

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37

运行输出: 
这里写图片描述 
点击发送广播按钮 
这里写图片描述 
成功接收到广播消息


动态发送广播和注销广播

在这之前把下面这一段删除:

 <receiver
            android:name=".AgainMyReceiver"
            android:enabled="true"
            android:exported="true" >
  </receiver>
 
 
  • 1
  • 2
  • 3
  • 4
  • 5

然后在布局文件添加两个按钮用于注册和注销广播: 
activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical"
    >


    <Button
        android:id="@+id/btnSend"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="发送广播"
        />

    <Button
        android:id="@+id/btnReg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="注册广播"
        />

    <Button
        android:id="@+id/btnUnreg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="注销广播"
        />

</LinearLayout>

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

接着在广播类添加此字段:

 public static final String ACTION = "com.xieth.as.againbroadcastreceiver.action.intent.AgainMyReceiver"
 
 
  • 1

MainActivity.java

package com.xieth.as.againbroadcastreceiver;

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

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private AgainMyReceiver myReceiver = null;

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

        findViewById(R.id.btnSend).setOnClickListener(this);
        findViewById(R.id.btnReg).setOnClickListener(this);
        findViewById(R.id.btnUnreg).setOnClickListener(this);


    }

    @Override
    public void onClick(View v) {

        int id = v.getId();
        switch (id) {
            case R.id.btnSend:
                Intent i = new Intent(AgainMyReceiver.ACTION);
                sendBroadcast(i);
                break;
            case R.id.btnReg:
                //注册广播
                if (myReceiver == null) {
                    myReceiver = new AgainMyReceiver();
                    registerReceiver(myReceiver,  registerReceiver(myReceiver, new IntentFilter(myReceiver.ACTION)););
                }
                break;
            case R.id.btnUnreg:
                //注销广播
                if (myReceiver != null) {
                    unregisterReceiver(myReceiver);
                    myReceiver = null;
                }
                break;
            default:
                break;
        }

    }
}

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53

运行效果: 
这里写图片描述 
刚开始点击发送广播,没有输出信息,所以我们在此注册广播,然后再点击发送广播: 
这里写图片描述


广播的优先级

再次新建一个广播类,命名为AgainMyReceiver1.java

package com.xieth.as.againbroadcastreceiver;

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

public class AgainMyReceiver1 extends BroadcastReceiver {
    public AgainMyReceiver1() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d("AgainMyReceiver", "AgainMyReceiver1接收到了消息");
    }
}

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

AgainMyReceiver.java

package com.xieth.as.againbroadcastreceiver;

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

public class AgainMyReceiver extends BroadcastReceiver {

    public static final String ACTION = "com.xieth.as.againbroadcastreceiver.action.intent.AgainMyReceiver";

    public AgainMyReceiver() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {

        Log.d("AgainMyReceiver", "AgainMyReceiver接收到了消息");

    }
}

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

然后修改AndroidManifest.xml文件

 <receiver
            android:name=".AgainMyReceiver1"
            android:enabled="true"
            android:exported="true"
            >
            <intent-filter>

                <action android:name="com.xieth.as.againbroadcastreceiver.action.intent.AgainMyReceiver"></action>

            </intent-filter>
        </receiver>
        <receiver
            android:name=".AgainMyReceiver"
            android:enabled="true"
            android:exported="true"
            >
            <intent-filter>

                <action android:name="com.xieth.as.againbroadcastreceiver.action.intent.AgainMyReceiver"></action>

            </intent-filter>
        </receiver>
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

先不设置优先级,观察输出结果: 
这里写图片描述 
由于action是一样的,所以同时接收到了消息,现在设置优先级,修改AndroidManifest.xml文件

<receiver
            android:name=".AgainMyReceiver1"
            android:enabled="true"
            android:exported="true"
            >
            <!--设置优先级-->
            <intent-filter android:priority="1">

                <action android:name="com.xieth.as.againbroadcastreceiver.action.intent.AgainMyReceiver"></action>

            </intent-filter>
        </receiver>
        <receiver
            android:name=".AgainMyReceiver"
            android:enabled="true"
            android:exported="true"
            >
            <!--设置优先级-->
            <intent-filter android:priority="2">

                <action android:name="com.xieth.as.againbroadcastreceiver.action.intent.AgainMyReceiver"></action>

            </intent-filter>
        </receiver>
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

然后运行输出: 
这里写图片描述 
可以看到AgainMyReceiver优先接收到消息,因为优先级为2,比AgainMyReceiver1的优先级高。


中断广播的发送

我们可以中断广播的发送: 
但是发送的方式不一样:是sendOrderedBroadcast()方式发送 
修改代码:

 case R.id.btnSend:
                Intent i = new Intent(AgainMyReceiver.ACTION);
                sendOrderedBroadcast(i, null);
//                sendBroadcast(i);
                break;
 
 
  • 1
  • 2
  • 3
  • 4
  • 5

然后在AgianMyReceiver加入如下代码终止比它优先级低发送广播 
使用abortBroadcast()方法 
其他不做修改!

 @Override
    public void onReceive(Context context, Intent intent) {

        Log.d("AgainMyReceiver", "AgainMyReceiver接收到了消息");
        abortBroadcast();

    }
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

运行输出: 
这里写图片描述 
可以看见成功终止了向AgainMyReceiver1发送广播!


2016年1月13日12:22:45 记录

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值