22 广播的发送

  • 有序广播
    • 不可被打断
  • 无序广播
    • 其他广播接受者可以修改在发送或者拦截发送
  • 粘性广播
    • 弃用 将广播放入广播池中等候处理为止

结构:
这里写图片描述

清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.qf.day22_broadcastreceiver_demo3"
    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.qf.day22_broadcastreceiver_demo3.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.qf.day22_broadcastreceiver_demo3.MyBroadCastReceiver01">
            <intent-filter 
                android:priority="20">
                <action android:name="cctv.hlj.beijin"/>
            </intent-filter>
        </receiver>
        <receiver android:name="com.qf.day22_broadcastreceiver_demo3.MyBroadCastReceiver02">
            <intent-filter 
                android:priority="10">
                <action android:name="cctv.hlj.beijin"/>
            </intent-filter>
        </receiver>
        <receiver android:name="com.qf.day22_broadcastreceiver_demo3.MyBroadCastReceiver03">
            <intent-filter
                  android:priority="30" >
                <action android:name="cctv.hlj.beijin"/>
            </intent-filter>
        </receiver>
    </application>

</manifest>

MainActivity.java

package com.qf.day22_broadcastreceiver_demo3;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void MySendClick(View v){

        Intent intent = new Intent();
        intent.setAction("cctv.hlj.beijin");
        intent.putExtra("str", "iphone7");
        //普通广播
        //sendBroadcast(intent);
        //有序广播1

        //sendOrderedBroadcast(intent, null);

        //有序广播2 并设置最终接受者 不管abortBroadcast();依然最后收到
        sendOrderedBroadcast(intent, null,new BroadcastReceiver(){

            @Override
            public void onReceive(Context context, Intent intent) {
                // TODO Auto-generated method stub

            }

        }, null, 0, null, null);
    }

}

MyBroadCastReceiver01.java

package com.qf.day22_broadcastreceiver_demo3;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;


public class MyBroadCastReceiver01 extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        if(intent!=null){
            String str = intent.getStringExtra("str");
            Toast.makeText(context, "===111=="+str, 0).show();
        }
    }

}

MyBroadCastReceiver02.java
与上代码差不多

MyBroadCastReceiver03.java:

package com.qf.day22_broadcastreceiver_demo3;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;


public class MyBroadCastReceiver03 extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        if(intent!=null){
            String str = intent.getStringExtra("str");
            Toast.makeText(context, "===333=="+str, 0).show();
        }

        abortBroadcast();//中断广播
    }

}

结果:只有MyBroadCastReceiver03吐司 还有最终接受者收到信息

posted on 2016-09-21 19:34  木鱼哥  阅读(118)  评论(0编辑  收藏  举报

导航