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

  • 相关阅读:
    spring注解事务管理
    Spring切入点表达式常用写法
    JPA和事务管理
    maven仓库
    struts2拦截器
    js中 转义字符
    jquery中变量加$和不加$有什么区别!
    spring大乱炖
    第一章 java多线程
    CS:APP 05 笔记
  • 原文地址:https://www.cnblogs.com/muyuge/p/6152196.html
Copyright © 2011-2022 走看看