当前位置:   article > 正文

发送有序广播

发送有序广播

任务描述:

(1)功能描述:实现拦截一条有序广播
(2)技术要点:通过sendOrderedMyBroadcast()方法发送一条有序广播
(3)实现步骤:设置优先级广播接收者的优先级

一、用户交互界面的设计与实现

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/stitch_one"
    tools:context="com.example.bz0209.m_application.MainActivity">
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="80dp"
        android:onClick="send"
        android:text="发送有序广播"
        android:paddingLeft="5dp"
        android:paddingRight="5dp"
        android:background="#FBFBFF"
        android:textSize="20sp"
        />
</RelativeLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

这里写图片描述

二、界面交互代码的设计与实现

package com.example.bz0209.m_application;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void send(View view){
        Intent intent =new Intent();
        //定义广播事件类型
        intent.setAction("Intercept_Stitch");
        //发送广播
        sendBroadcast(intent,null);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

三、创建三个广播接收者

(1)MyBroadcastReceiverOne.java

package com.example.bz0209.m_application;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class MyBroadcastReceiverOne extends BroadcastReceiver {
    public MyBroadcastReceiverOne() {
    }
    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO: This method is called when the BroadcastReceiver is receiving
        // an Intent broadcast.
        Log.i("MyBroadcastReceiverOne","自定义广播接收者One,接收到了广播事件");
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

(2)MyBroadcastReceiverTwo.java

package com.example.bz0209.m_application;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class MyBroadcastReceiverTwo extends BroadcastReceiver {
    public MyBroadcastReceiverTwo() {
    }
    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO: This method is called when the BroadcastReceiver is receiving
        // an Intent broadcast.
        Log.i("MyBroadcastReceiverTwo","自定义广播接收者Two,接收到了广播事件");
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

(3)MyBroadcastReceiverThree.java

package com.example.bz0209.m_application;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class MyBroadcastReceiverThree extends BroadcastReceiver {
    public MyBroadcastReceiverThree() {
    }
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i("自定义广播接收者Three,接收到了广播事件", "MyBroadcastReceiverThree");
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

四、在清单文件中注册

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.bz0209.m_application">
    <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=".MyBroadcastReceiverOne">
            <intent-filter android:priority="1000">
                <action android:name="Intercept_Stitch"/>
            </intent-filter>
        </receiver>
        <receiver android:name=".MyBroadcastReceiverTwo">
            <intent-filter android:priority="200">
                <action android:name="Intercept_Stitch"/>
            </intent-filter>
        </receiver>
        <receiver android:name=".MyBroadcastReceiverThree">
            <intent-filter android:priority="600">
                <action android:name="Intercept_Stitch"/>
            </intent-filter>
        </receiver>
    </application>
</manifest>
  • 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

五、结果展示

这里写图片描述
(1)程序启动后,单击“发送有序广播”按钮,发出一条广播事件
这里写图片描述
(2)若将广播接收者MyBroadcastReceiverTwo优先级同样设置为1000,并将MyBroadcastReceiverTwo注册在MyBroadcastReceiverOne前面,再来运行程序
这里写图片描述
(3)修改MyBroadcastReceiverTwo如下:

package com.example.bz0209.m_application;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class MyBroadcastReceiverTwo extends BroadcastReceiver {
    public MyBroadcastReceiverTwo() {
    }
    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO: This method is called when the BroadcastReceiver is receiving
        // an Intent broadcast.
        Log.i("MyBroadcastReceiverTwo","自定义广播接收者Two,接收到了广播事件");
        abortBroadcast();
        Log.i("MyBroadcastReceiverTwo","广播被我终结了");
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

结果:
这里写图片描述

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/小舞很执着/article/detail/902101
推荐阅读
相关标签
  

闽ICP备14008679号