BroadcastReceiver的简单使用

本文深入探讨了Android中BroadcastReceiver组件的使用方法,包括普通广播和有序广播的实现过程及区别,通过代码实例展示了如何在Activity间进行广播通信。

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

BroadcastReceiver 是android4大组件之一,它的使用是非常重要的。最常用的是普通广播和有序广播。
广播的机制是:发送广播->接收广播。

//activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/bt_send1"
        android:layout_marginTop="20dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="发送一条普通广播" />

    <Button
        android:id="@+id/bt_send2"
        android:layout_marginTop="20dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="发送一条有序广播" />

</LinearLayout>

//MainActivity.java

package com.example.test_broadcastreceiver;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener{

    private Button bt_send1;
    private Button bt_send2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bt_send1=(Button) findViewById(R.id.bt_send1);
        bt_send2=(Button) findViewById(R.id.bt_send2);
        bt_send1.setOnClickListener(this);
        bt_send2.setOnClickListener(this);

        IntentFilter intentFilter=new IntentFilter("BC_One");
        BC2 bc2=new BC2();
        //动态注册广播接收机
        registerReceiver(bc2, intentFilter);
    }
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
        case R.id.bt_send1:
            Intent intent=new Intent();
            //intent携带的信息
            intent.putExtra("msg","这是一条普通广播");
            //为intent设置Action
            intent.setAction("BC_One");
            //发送普通广播
            sendBroadcast(intent);
            break;

        case R.id.bt_send2:
            Intent intent2=new Intent();
            //intent携带的信息
            intent2.putExtra("msg","这是一条有序广播");
            //为intent设置Action
            intent2.setAction("BC_One");
            //发送有序广播
            sendOrderedBroadcast(intent2, null);
            break;

        default:
            break;
        }
    }


}

//BC1.java

package com.example.test_broadcastreceiver;

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

public class BC1 extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        //获取信息
        String msg=intent.getStringExtra("msg");
        System.out.println("普通接收机1收到信息:"+msg);

        Bundle bundle=getResultExtras(true);
        String test=bundle.getString("test");
        System.out.println("普通接收机1收到从高级别广播传播的信息:"+test);
    }

}

//BC2.java

package com.example.test_broadcastreceiver;

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

public class BC2 extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        //获取信息
        String msg=intent.getStringExtra("msg");
        System.out.println("普通接收机2收到信息:"+msg);
        //截断广播,低级别的将收不到广播,这里只对有序广播有效,普通传播不能截止。
        //abortBroadcast();

        //进行传播数据,这里只对有序广播有效,普通传播不能截止。
        Bundle bundle=new Bundle();
        bundle.putString("test","广播传递处理的数据");
        setResultExtras(bundle);
    }

}

//AndroidManifest.xml

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.test_broadcastreceiver.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>



        <!-- 静态注册BroadcastReceiver -->
        <receiver android:name="com.example.test_broadcastreceiver.BC1">
            <intent-filter >       <!-- 可以在filter属性里面加上广播级别。    android:priority="100" -->
                <action android:name="BC_One"/>
            </intent-filter>
        </receiver>

        <!--       
        <receiver android:name="com.example.test_broadcastreceiver.BC2">
            <intent-filter
                android:priority="100" >
                <action android:name="BC_One"/>
            </intent-filter>
        </receiver> 
                    -->

    </application>

</manifest>

运行结果如图:

//普通广播
1.同等级的情况下,动态注册高于静态注册。
2.不能进行广播处理和截止广播。
运行结果如图:
这里写图片描述
动态注册的接收机2先收到信息,但是不能传播处理信息。

//同步广播
1.同等级的情况下,动态注册高于静态注册。
2.能进行广播处理和截止广播,从高到低进行。可以在filter属性里面加上广播级别。 android:priority=”100” 从-1000—1000.
运行结果如图:
这里写图片描述
动态注册的接收机2先收到信息,传播处理信息给静态注册的接收机1。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值