安卓之broadcastreceiver的使用、动态注册和注销、broadcastreceiver的优先级

AndroidMaifest.xml的文件如下:

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

    <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/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=".MyReceiver">
            <intent-filter android:priority="10">
                <action android:name="com.example.helios.learnbroadcastreceiver.intent.action.MyReceiver" />
            </intent-filter>
        </receiver>
        <receiver android:name=".MyReceiver2">
            <intent-filter android:priority="8">
                <action android:name="com.example.helios.learnbroadcastreceiver.intent.action.MyReceiver" />
            </intent-filter>
        </receiver>
    </application>

</manifest>

布局界面就三个按钮,发送消息,动态注册和动态注销

MainActivity.java代码如下

package com.example.helios.learnbroadcastreceiver;

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.Button;
import android.widget.Toast;

/**
 * 使用BroadcastReceiver
 *
 *
 * 动态注册和注销BroadcastReceiver
 * 动态注册和注销时,删除掉Androidmaindest.xml里面的
 * <receiver
 android:name=".MyReceiver"
 android:enabled="true"
 android:exported="true"></receiver>
 *
 * BroadcastReceiver的优先级
 *android:priority="10"
 */
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    Button btn_send,btn_reg,btn_unreg;
    private MyReceiver receiver;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn_send = this.findViewById(R.id.btn_send);
        btn_reg = this.findViewById(R.id.btn_reg);
        btn_unreg = this.findViewById(R.id.btn_unreg);
        btn_send.setOnClickListener(this);
        btn_reg.setOnClickListener(this);
        btn_unreg.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch(view.getId()){
            case R.id.btn_send:
               // Intent intent = new Intent(MainActivity.this,MyReceiver.class);
                //动态注册和注销broadcastreceiver的时候,不能使用显式intent的方法,而要用隐式intent的方法
                Intent intent = new Intent(MyReceiver.ACTION);
                intent.putExtra("data","hello,我是第一次使用broadcast!!!!");
                sendBroadcast(intent);
                Toast.makeText(MainActivity.this,"请先注册广播接收器!!!",Toast.LENGTH_SHORT).show();
                break;
                //注册广播接收器
            case R.id.btn_reg:
                if(receiver == null){
                    receiver = new MyReceiver();
                    Toast.makeText(MainActivity.this,"注册广播接收器成功!!!",Toast.LENGTH_SHORT).show();
                    registerReceiver(receiver,new IntentFilter(MyReceiver.ACTION));
                }
                break;
            //注销广播接收器
            case R.id.btn_unreg:
                if(receiver != null){
                    unregisterReceiver(receiver);
                    Toast.makeText(MainActivity.this,"注销广播接收器成功!!!",Toast.LENGTH_SHORT).show();
                    receiver = null;
                }
                break;
        }
    }
}

MyReceiver的代码如下:

package com.example.helios.learnbroadcastreceiver;

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


public class MyReceiver extends BroadcastReceiver {
    public static final String ACTION = "com.example.helios.learnbroadcastreceiver.intent.action.MyReceiver";
    public MyReceiver(){
    }


    @Override
    public void onReceive(Context context, Intent intent) {
        System.out.println("接收到了消息!!!消息的内容是:"+intent.getStringExtra("data"));


    }
}

MyReceiver2的代码如下:

package com.example.helios.learnbroadcastreceiver;

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

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

    @Override
    public void onReceive(Context context, Intent intent) {
        System.out.println("MyReceiver2 接收了到消息");

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值