实现有序广播

实现有序广播

首先我们需要注意以下几点:

      

  • 第一点:广播的接受是按照事先设定的优先级接收的
  • 第二点:优先级相同的广播接收者先被注册的先接收广播
  • 第三点:如果广播在传递过程中被拦截,则后面的广播则无法接受广播


 

通过设置不同的优先级查看广播接受者的接受顺序,或通过不同的注册顺序查看广播接受者的接受顺序
首先先写基本的布局文件和广播接收器
布局文件:

<?xml version="1.0" encoding="utf-8"?>
<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:background="@drawable/stitch_one"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.bz0209.myapplication_3.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
  • 1
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

Main_Activity:

package com.example.bz0209.myapplication_3;

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");
        sendOrderedBroadcast(intent,null);
    }
}

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

MyReceiverone(其他两个只是输出的不一样其他一样):

package com.example.bz0209.myapplication_3;

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

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

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO: This method is called when the BroadcastReceiver is receiving
        // an Intent broadcast.
        Log.i("one", "one 接收到了事件");
    }
}

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

AndroidManifest:

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

    <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=".MyReceiverone"
            android:enabled="true"
            android:exported="true">
            <intent-filter android:priority="1000">
                <action android:name="Intercept_Stitch"/>
            </intent-filter>
        </receiver>
        <receiver
            android:name=".MyReceivertwo"
            android:enabled="true"
            android:exported="true" >
            <intent-filter android:priority="200">
                <action android:name="Intercept_Stitch"/>
            </intent-filter>
        </receiver>
        <receiver
            android:name=".MyReceiverthree"
            android:enabled="true"
            android:exported="true">
            <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
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45

注意上面的Manifest文件中不同receiver的优先级
1. 正常运行测试
 
我们通过日志的打印信息可以看到广播的接受是按照事先设定的优先级接收的。
2. 我们把MyReceivertwo的优先级同样设置成为1000并且把注册信息移动到MyReceiverone前面,运行效果如图:
 
我们发现优先级相同的广播接收者先被注册的先接收广播
3. 我们把MyReceivertwo修改为下面图片:

因为广播在MyReceivertwo被拦截,所以MyReceiverone和MyReceiverthree接收不到广播!!!

然后运行测试:

我们看到确实是这样。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Kotlin中,可以使用BroadcastReceiver来接收有序广播有序广播可以按照优先级依次传递给不同的接收器,这种方式可以确保广播按照特定的顺序进行处理。 以下是接收有序广播的示例代码: 1. 创建一个BroadcastReceiver类并实现onReceive方法: ``` class MyReceiver : BroadcastReceiver() { override fun onReceive(context: Context?, intent: Intent?) { val message = intent?.getStringExtra("message") Log.d("MyReceiver", "Received message: $message") // 处理广播消息 // ... // 终止广播传递(如果需要) // abortBroadcast() } } ``` 2. 在AndroidManifest.xml文件中声明广播接收器: ``` <receiver android:name=".MyReceiver"> <intent-filter> <action android:name="com.example.ACTION_MY_BROADCAST" /> </intent-filter> </receiver> ``` 3. 发送有序广播: ``` val intent = Intent("com.example.ACTION_MY_BROADCAST") intent.putExtra("message", "Hello, world!") sendOrderedBroadcast(intent, null) ``` 在这个示例中,我们创建了一个BroadcastReceiver类来处理接收到的广播消息。在onReceive方法中,我们可以获取广播消息的内容,并进行相应的处理。如果需要,我们还可以使用abortBroadcast()方法终止广播传递。 在AndroidManifest.xml文件中,我们声明了我们的广播接收器并指定了要接收的广播消息的名称(即com.example.ACTION_MY_BROADCAST)。 最后,我们使用sendOrderedBroadcast方法发送有序广播。如果有多个接收器注册了相同的广播消息,它们将按照优先级依次接收到广播消息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值