• android 之 广播


    如何使用android进行自定义的广播事件?

    自定义发送广播的分类

    1. 有序广播
    2. 无序广播
    ###1.什么是无序广播? ###无序广播:通过Context.sendBroadcast()方法来发送,它是完全异步的。通俗点理解就是我发送出去之后,有没有人接收这个广播就与发送者没有关系了。反正发送者发送出去了 ``` //android中四大组件都要在清单文件中配置一下 //这个intent-filter(意图过滤器)是最重要的一个,配置intent-filter的功能是实现接收什么样的广播(都由过滤器来实现该方法) ```
        
    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }
    
        //点击按钮发送一条无序广播
        public void click(View view)
        {
            Intent intent = new Intent();
    
            intent.setAction("com.itheima.custom");//设定意图之后就可以使用指定广播接收者进行接收。
    
            intent.putExtra("name","新闻联播,每天晚上准时开播");
    
            sendBroadcast(intent);//发送无序广播
    
            Toast.makeText(this,"successful",Toast.LENGTH_LONG).show();
        }
    }
    

    接收方的代码

        //该方法是接收自定义发送的广播
       public class ReveiverCoust extends BroadcastReceiver {
    
        @Override
        public void onReceive(Context context, Intent intent) {
            String content = intent.getStringExtra("name");
    
            Toast.makeText(context,content,Toast.LENGTH_LONG).show();
        }
    }
    

    2.什么是有序广播?

    该广播是通过Context.sendOrderBroadcast()来发送的,所有的receiver依次执行,(该广播是有优先级的,按照优先级进行执行)

        //有序广播如何发送数据呢?
        //有序广播使用的也是Intent进行发送数据,但是使用的发送函数有所不同
        Intent intent = new Intent();
    
        intent.setAction("想要被那个接收器所识别的广播器");
        
        //发送有序广播所使用的方法函数是sendOrderedBroadcast()的方法进行发送相应的内容
        /* sendOrderedBroadcast的各各参数
            * 参数1.intent 意图
            *
            * 参数2.receiverPermission :接收的权限
            *
            * 参数3.resultReceiver:最终的Receiver
            *
            * 参数4.scheduler
            *
            * 参数5.initialCode   :int类型,初始码
            *
            * 参数6.initialData   :初始化数据
            *
            *
            * 参数7.initialExtras   :是否有额外的数据
            * */
            sendOrderedBroadcast(intent,null,new FinalReceiver(),null,1,"发送大米",null);
            Toast.makeText(this,"successful",Toast.LENGTH_SHORT).show();
        
    
        //有序广播中的接收器是如何注册的
        <receiver
                android:name=".AReceiver"
                android:enabled="true"
                android:exported="true" >
                <intent-filter android:priority="1000">//因为有序广播的按照优先级的顺序进行接收广播的,所以设置优先级的方式是android:priority进行设置:取值的范围是-1000-1000
                    <action android:name="com.ithema.sendrice"/>
                </intent-filter>
            </receiver>
            <receiver
                android:name=".BReceiver"
                android:enabled="true"
                android:exported="true" >
                <intent-filter android:priority="500">
                    <action android:name="com.ithema.sendrice"/>
                </intent-filter>
            </receiver>
            <receiver
                android:name=".CReceiver"
                android:enabled="true"
                android:exported="true">
                <intent-filter android:priority="0">
                    <action android:name="com.ithema.sendrice"/>
                </intent-filter>
            </receiver>
    
        //以其中的一个接收器为例
        public class AReceiver extends BroadcastReceiver {
    
        @Override
        public void onReceive(Context context, Intent intent) {
           //有序广播的接收的方法
            String content = getResultData();//通过使用getResultData的方法获取广播中的信息
            Toast.makeText(context,content,Toast.LENGTH_SHORT).show();
    
            //终止广播:
            //abortBroadcast();//终止广播,将广播停下播放,一旦终止之后,有序广播就不再继续向下传播了。
            setResultData("");//通过setResultData的方法设置广播中的信息,有序广播的内容可以被修改
            }
    }
    

    有序广播中的特殊的参数

    resultReceiver:该参数的含义是最终的广播,经过发送广播之后,通过该参数可以得到最后广播的结果。

        //该广播不用在清单文件中注册
        public class onReceiver extends BroadcastReceiver
        {
            public void onReceive(Context context,Intent intent)
            {
                String content = getResultData();
    
                Toast.makeText(context,context,Toast.LENGTH_SHORT).show();
            }
        }
    

    有序广播和无序广播的特点

    有序广播

    1. 有序广播可以被终止(abortBroadcast()将广播终止)
    2. 有序广播的内容可以被修改(使用getResultData()获取广播中的内容,使用setResultData()进行对广播中的内容进行设置)
    ####无序广播
    • 无序广播不可以被终止
    • 无序广播的内容不可以被修改

    如何区分系统的广播是有序广播还是无序广播的方法?

        public void onReceive(Context context ,Intent intent)
        {
            验证系统的广播是有序的还是无序广播的方法是
            使用//abortBroadcast()
            使用上述的方法之后如还能收到广播则说明是无序广播,若没有收到则是有序广播
    
    
            //若是无序广播则使用String content = intent.getStringExtra("设置的键值的名称");接收发送的数据
            
            //若是使用有序广播则使用Strig content = getResultData()获取广播中的信息
    
            
        }
    

    附加的内容(特殊的广播接收者):

    特殊的广播接收者(一天中使用的非常的频繁)在清单中注册是无效的:例如屏幕的解锁和锁屏在清单中注册是无效的。

    注册广播的两种方式:

    1. 动态注册 通过代码方式注册
    2. 静态注册 在清单文件通过receiver tag结点发布

    动态的注册方式:

        //动态注册的基本步骤:
        ScreenReceive screenReceive = new ScreenReceive();
        IntentFilter intentFilter = new IntentFilter();
    
        intentFilter.addAction("android.intent.action.SCREEN_OFF");
        intentFIlter.addAction("android.intent.action.SCREEN_ON");
    
        registerReceiver(screenReceive,intentFilter);
    
  • 相关阅读:
    第05篇:C#星夜拾遗之使用数据库 拓荒者
    第02篇:C#星夜拾遗之Windows窗体 拓荒者
    移动“我的文档” 2010年5月22日学习笔记(1) 拓荒者
    为Windows Live Writer写一个简单的插件 拓荒者
    [转] 关于VisualC++的ATL、MFC、CLR对比 拓荒者
    如何在Windows 2003 中使用Windows Live Writer? 2010年5月21日学习笔记(1) 拓荒者
    第01篇:C#星夜拾遗之如何开始C#学习 拓荒者
    JavaScript Mobile开发框架汇总
    CSS调用远程字体
    locale的详细解释
  • 原文地址:https://www.cnblogs.com/gxcstyle/p/6937288.html
Copyright © 2020-2023  润新知