zoukankan      html  css  js  c++  java
  • 发送一条有序广播

    //点击按钮  发送大米 
        public void click(View v) {
            
            //发送一条有序广播 
            Intent intent = new Intent();
            //设置一个action
            intent.setAction("com.itheima.rice");
            /**
             * receiverPermission 接收的权限
             * resultReceiver 最为一个最终的receive  最后都能够接收到广播 
             */
            sendOrderedBroadcast(intent, null, new FinalReceiver(), null, 1, "给每个村民发了1000斤大米", null);
            
            
        }
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.widget.Toast;
    
    public class ProvienceReceiver extends BroadcastReceiver {
    
        @Override
        public void onReceive(Context context, Intent intent) {
            
            //[1]获取到发送广播携带的数据 
            String content = getResultData();
            
            //[2]展示到Toast上
            Toast.makeText(context, "省:"+content, 1).show();
            
            //[2.1]终止广播 
    //        abortBroadcast();
            
            //[3]修改数据 (扣留大米)
            setResultData("给每个村民发了500斤大米");
            
            
        }
    
    }
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.widget.Toast;
    
    public class CityReceiver extends BroadcastReceiver {
    
        @Override
        public void onReceive(Context context, Intent intent) {
    
            //[1]获取到发送广播携带的数据 
            String content = getResultData();
            
            //[2]展示到Toast上
            Toast.makeText(context, "市:"+content, 1).show();
            
            //[3]修改数据 (扣留大米)
            setResultData("给每个村民发了200斤大米");
            
        }
    
    }
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.widget.Toast;
    
    public class CountryReceiver extends BroadcastReceiver {
    
        @Override
        public void onReceive(Context context, Intent intent) {
    
            //[1]获取到发送广播携带的数据 
            String content = getResultData();
            
            //[2]展示到Toast上
            Toast.makeText(context, "乡:"+content, 1).show();
    
            //[3]修改数据 (扣留大米)
            setResultData("给每个村民发了10斤大米");
            
        }
    
    }
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.widget.Toast;
    
    public class NongMinReceiver extends BroadcastReceiver {
    
        @Override
        public void onReceive(Context context, Intent intent) {
            
            //[1]获取到发送广播携带的数据 
            String content = getResultData();
            
            //[2]展示到Toast上
            Toast.makeText(context, "农民:"+content, 1).show();
        }
    
    }
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.widget.Toast;
    
    /**
     * 最终的receiver 不需要再清单文件里面配置
     * @author jhon
     *
     */
    public class FinalReceiver extends BroadcastReceiver {
    
        @Override
        public void onReceive(Context context, Intent intent) {
    
            String content = getResultData();
    
            Toast.makeText(context, "报告"+content, 1).show();
            
            
        }
    
    }
         <!--配置广播接收者  -->
            <receiver android:name="com.itheima.receiverice.ProvienceReceiver">
                <intent-filter 
                    android:priority="1000"
                    >
                    <action android:name="com.itheima.rice"/>
                </intent-filter>
            </receiver>
            
             <receiver android:name="com.itheima.receiverice.CityReceiver">
                <intent-filter 
                    android:priority="800"
                    >
                    <action android:name="com.itheima.rice"/>
                </intent-filter>
            </receiver>
            
                 <receiver android:name="com.itheima.receiverice.CountryReceiver">
                <intent-filter 
                    android:priority="100"
                    >
                    <action android:name="com.itheima.rice"/>
                </intent-filter>
            </receiver>
             <receiver android:name="com.itheima.receiverice.NongMinReceiver">
                <intent-filter 
                    android:priority="10"
                    >
                    <action android:name="com.itheima.rice"/>
                </intent-filter>
            </receiver>
  • 相关阅读:
    Redis连接池的介绍和原理
    Golang操作第三方开源Redis库
    Redis的五大数据类型和CRUD操作
    Redis的基本使用
    Redis数据库的基本介绍和安装
    Golang基于TCP协议实现简单的server和client聊天
    Golang反射中的Type和Kind的区别
    Golang中的常量
    Golang对基本数据类型和结构体进行反射
    Vue 使用lodash库减少watch对后台请求压力
  • 原文地址:https://www.cnblogs.com/xufengyuan/p/5957827.html
Copyright © 2011-2022 走看看