四大组件之BroadcastReceiver

组件篇BroadcastReceiver:

普通广播的特点:

同级别接收是随机的(无序的)

级别低的后收到广播

接收器不能截断广播的继续传播也不能处理广播

同级别动态注册高于静态注册

package com.example.broadcastrecieverdemo;

import android.app.Activity;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;


public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //动态注册
        IntentFilter intentFilter=new IntentFilter("BC_action");
        BC2 bc2=new BC2();
        registerReceiver(bc2,intentFilter);
    }
    public void doClick(View view){
    	switch (view.getId()) {
		case R.id.send_btn:
			Intent intent =new Intent();
			intent.putExtra("msg","这是一条普通广播");
			intent.setAction("BC_action");
			sendBroadcast(intent);
			break;

		default:
			break;
		}
    }
}

package com.example.broadcastrecieverdemo;

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

public class BC1 extends BroadcastReceiver{

	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
		String s=intent.getStringExtra("msg");
		System.out.println("receiver1收到消息:"+s);
	}

}

package com.example.broadcastrecieverdemo;

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

public class BC2 extends BroadcastReceiver{

	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
		String s=intent.getStringExtra("msg");
		System.out.println("receiver2收到消息:"+s);
	}

}

 <!--   静态注册 -->
        <receiver 
            android:name="com.example.broadcastrecieverdemo.BC1"
            >
            <intent-filter 
                android:priority="100"
                >
                <action 
                    android:name="BC_action"
                    />
            </intent-filter>
        </receiver>
        <!-- <receiver 
            android:name="com.example.broadcastrecieverdemo.BC2"
            >
            <intent-filter 
                android:priority="200"
                >
                <action 
                    android:name="BC_action"
                    />
            </intent-filter>
        </receiver> -->



有序广播的特点:

同级别接受顺序是随机的

能截断广播的继续传播,高级别的广播接收器收到该广播后可以决定是否把该广播截断

接收器能截断广播的继续传播,也能处理广播


package com.example.broadcastrecieverdemo;

import android.app.Activity;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;


public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //动态注册
//        IntentFilter intentFilter=new IntentFilter("BC_action");
//        BC2 bc2=new BC2();
//        registerReceiver(bc2,intentFilter);
    }
    public void doClick(View view){
    	switch (view.getId()) {
		case R.id.send_btn:
			Intent intent =new Intent();
			intent.putExtra("msg","这是一条普通广播");
			intent.setAction("BC_action");
			sendBroadcast(intent);
			break;
		case R.id.send_btn1:
			Intent intent1 =new Intent();
			intent1.putExtra("msg","这是一条有序广播");
			intent1.setAction("BC_action");
			sendOrderedBroadcast(intent1,null);
			break;
		default:
			break;
		}
    }
}

package com.example.broadcastrecieverdemo;

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 s=intent.getStringExtra("msg");
		System.out.println("receiver1收到消息:"+s);
		//终止广播
		//abortBroadcast();
		Bundle bundle=new Bundle();
		bundle.putString("test", "广播处理数据");
		setResultExtras(bundle);
	}

}

package com.example.broadcastrecieverdemo;

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 s=intent.getStringExtra("msg");
		System.out.println("receiver2收到消息:"+s);
		Bundle bundle=getResultExtras(true);
		String s2=bundle.getString("test");
		System.out.println("得到的结果是:"+s2);
	}

}

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    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:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.broadcastrecieverdemo.MainActivity" >

    <Button
        android:id="@+id/send_btn"
        android:onClick="doClick"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="发送一条广播" />

<Button
    android:id="@+id/send_btn1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="81dp"
    android:onClick="doClick"
    android:text="发送一条有序广播" />

</RelativeLayout>

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

    <uses-sdk
        android:minSdkVersion="15"
        android:targetSdkVersion="20" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".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>
     <!--   静态注册 -->
        <receiver 
            android:name="com.example.broadcastrecieverdemo.BC1"
            >
            <intent-filter >
                <action 
                    android:name="BC_action"
                    />
            </intent-filter>
        </receiver>
         <receiver 
            android:name="com.example.broadcastrecieverdemo.BC2"
            >
            <intent-filter >
                <action 
                    android:name="BC_action"
                    />
            </intent-filter>
        </receiver>
    </application>

</manifest>



异步广播:

不能将处理结果传给下一个接受着,无法终止广播

bc3为全局变量;

Intent intent2 =new Intent();
			intent2.putExtra("msg","这是一条有序广播");
			intent2.setAction("BC_3");
			sendStickyBroadcast(intent2);
			IntentFilter intentFilter=new IntentFilter("BC_3");
			bc3=new BC3();
			registerReceiver(bc3,intentFilter);

package com.example.broadcastrecieverdemo;

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

public class BC3 extends BroadcastReceiver{

	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
		System.out.println("接收到了异步广播");
	}

}

注:

<uses-permission android:name="android.permission.BROADCAST_STICKY"/>

动态注册需要卸载掉:

protected void onDestroy() {
    	// TODO Auto-generated method stub
    	super.onDestroy();
    	unregisterReceiver(bc3);
    	System.err.println("123");
    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值