zoukankan      html  css  js  c++  java
  • 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吐司 还有最终接受者收到信息

  • 相关阅读:
    ZeptoLab Code Rush 2015
    UVa 10048 Audiophobia【Floyd】
    POJ 1847 Tram【Floyd】
    UVa 247 Calling Circles【传递闭包】
    UVa 1395 Slim Span【最小生成树】
    HDU 4006 The kth great number【优先队列】
    UVa 674 Coin Change【记忆化搜索】
    UVa 10285 Longest Run on a Snowboard【记忆化搜索】
    【NOIP2016提高A组模拟9.28】求导
    【NOIP2012模拟10.9】电费结算
  • 原文地址:https://www.cnblogs.com/muyuge/p/6152196.html
Copyright © 2011-2022 走看看