发送有序广播

1 篇文章 0 订阅
1 篇文章 0 订阅

任务描述:

(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>

这里写图片描述

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

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)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,接收到了广播事件");
    }
}

(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,接收到了广播事件");
    }
}

(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");
    }
}

四、在清单文件中注册

<?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)若将广播接收者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","广播被我终结了");
    }
}

结果:
这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android发送有序广播,可以使用 `sendOrderedBroadcast()` 方法。这个方法会按照指定的顺序发送广播给所有的接收器,确保接收器的顺序是一致的,并且可以通过设置优先级来控制接收器的顺序。 下面是一个简单的例子: ```java Intent intent = new Intent("com.example.MY_ACTION"); intent.putExtra("message", "Hello, world!"); // 设置广播接收器的顺序 List<String> receiverPermissions = new ArrayList<>(); receiverPermissions.add("com.example.permission.RECEIVE_MY_ACTION"); receiverPermissions.add("android.permission.RECEIVE_MY_ACTION"); sendOrderedBroadcast(intent, receiverPermissions, new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { // 处理接收到的广播 String message = intent.getStringExtra("message"); Log.d("MyApp", "Received broadcast message: " + message); } }, null, Activity.RESULT_OK, null, null); ``` 在这个例子中,我们创建了一个名为 `com.example.MY_ACTION` 的广播,并设置了一个包含消息的额外数据。然后我们调用 `sendOrderedBroadcast()` 方法,并传递接收器的顺序列表、一个广播接收器,以及其他参数。最后,我们在广播接收器的 `onReceive()` 方法中处理接收到的广播。 需要注意的是,发送有序广播需要指定接收器的顺序,这意味着你需要在应用程序的清单文件中为每个接收器定义一个优先级,以便系统可以按照正确的顺序发送广播
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值