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吐司 还有最终接受者收到信息

  • 相关阅读:
    Qt画笔实现折线图
    Qt动态布局
    ffmpeg录制流媒体,正常方式停止录制
    解决libvlc_media_player_stop时死锁的方法
    Ubuntu 16 修改时区!
    qt窗口最小化之后无法打开
    Qt 之 去除窗口部件被选中后的焦点虚线框
    WINDOWS中, 如何查看一个运行中的程序是64位还是32位的
    DHTMLX学习总结
    mui plus.uploader.createUpload 上传文件服务端获取文件名中文乱码问题
  • 原文地址:https://www.cnblogs.com/muyuge/p/6152196.html
Copyright © 2011-2022 走看看