Android中广播接收者

本文介绍了一个简单的有序广播实现案例,包括XML布局文件、MainActivity中按钮点击事件、三个广播接收者及配置清单文件设置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、首先创建xml文件界面代码如下:
<?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"  
    tools:context="cn.edu.bzu.test.MainActivity"  
    android:background="@drawable/stitch_one"  
    >  
  
    <Button  
        android:text="发送有序广播"  
        android:onClick="send"  
        android:background="#FFFFFFFF"  
        android:textSize="25sp"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_marginTop="116dp"  
        android:id="@+id/button"  
        android:layout_alignParentTop="true"  
        android:layout_centerHorizontal="true" />  
</RelativeLayout>  

二、在MainActivity中编写button按钮的点击事件send()


package cn.edu.bzu.test;  
  
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);  
    }  
}  
三、编写三个广播接收者:

package cn.edu.bzu.test;  
  
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,接收到了广播事件");  
    }  
}
其他两个和第一个一样将one改成two和three即可。

四、配置清单文件,代码如下:

<receiver  
            android:name=".MyBroadcastReceiverOne"  
            android:enabled="true"  
            android:exported="true">  
            <intent-filter android:priority="1000">  
                <action android:name="Intercept_Stitch"/>  
            </intent-filter>  
        </receiver>  
        <receiver  
            android:name=".MyBroadcastReceiverTwo"  
            android:enabled="true"  
            android:exported="true">  
            <intent-filter android:priority="1000">  
                <action android:name="Intercept_Stitch"/>  
            </intent-filter>  
        </receiver>  
        <receiver  
            android:name=".MyBroadcastReceiverThree"  
            android:enabled="true"  
            android:exported="true">  
            <intent-filter android:priority="600">  
                <action android:name="Intercept_Stitch"/>  
            </intent-filter>  
  
        </receiver>
priority代表广播接收者的优先级,后面的数字越大,优先级越高。

运行结果如下图:



按照一二三依次显示我们输入的文字


再将广播接收者two的优先级改成1000运行结果如下图:

 
广播接收者two成了第一个


将广播接收者three加几行代码:

abortBroadcast()
Log.i("MyBroadcastReceiverOne","自定义的广播接受者one,接收到了广播事件");
运行结果如下图:



广播接收者就会被拦截。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值