zoukankan      html  css  js  c++  java
  • Android基础之五:四大组件(Broadcast Receiver)

    • Broadcast Receiver作为Android四大组件之一,在整个系统中广泛运用,系统中存在各种各样的广播机制,例如下载,网络等都有具体的广播接收器
    • 广播在很大程度上简化了开发,可以通过广播监听系统状态变化,监听另一App中事件传递,可以接收当前App中不同组件,不同UI、不同线程之间的消息传递
    • 广播的创建与使用
      • 创建一个继承自BroadcastReceiver类的子类,重写onReceiver方法
      • public class MReceiver extends BroadcastReceiver {
            @Override
            public void onReceive(Context context, Intent intent) {
                //ToDo
            }
        }
      • 注意在onReceive中不能处理耗时操作,一般不能超过6s,超过6s就会报Exception异常,如果有耗时操作,都放到辅助线程中运行,辅助线程具体参考Android基础之多线程开发
      • Manifest中需要注册该Receiver
        • receiver节点
          • 属性:name:指定receiver类路径
          • 子节点intent-filter
            • 属性:priority:优先级,-1000~1000,优先级高的能够早接收到广播,只有sendOrderedBroadcast发送才有用
            • 子节点
              • action:标识,为隐式Intent调用准备,可以跨应用接收广播
      • <receiver android:name=".MReceiver" android:exported="true">
             <intent-filter android:priority="100">
                 <action android:name="com.example.AndroidTest_01.receiver1"></action>
             </intent-filter>
        </receiver>
      • 完成注册后并不是在应用启动的时候广播接收器才启用,而是一直运行在系统中,不管应用是打开还是退出,都会启用,在接收到消息后都会执行onReceiver,尽管App已经退出了,因此这里如果需要与App进行交互,需要注意
      • 跨应用接收广播,或者接收系统广播需要设置android:exported=true;
      • 发送广播
        • 创建广播Intent
          • 显式创建Intent
          • Intent intent1=new Intent(MyActivity.this,MReceiver.class);
            sendBroadcast(intent1);
          • 隐式创建Intent
          • Intent intent1=new Intent();
            intent1.setAction("com.example.AndroidTest_01.receiver1");
            sendBroadcast(intent1);
        • 发送广播
          • 普通发送
          • sendBroadcast(intent1);
          • 有序发送
          • sendOrderedBroadcast(intent1,null);
          • 有序广播配合广播注册优先级使用,能够控制接收器接收顺序,并且在优先级高的接收器中可以停止对优先级低的接收器进行继续广播
          • @Override
            public void onReceive(Context context, Intent intent) {
                //ToDo
                abortBroadcast();
            }
          • 滞留广播发送StickyBroadcast、StickyOrderedBroadcast
            • 滞留广播具体表现为可以先发送消息,再注册接收,即先上船后补票
            • 当发送滞留广播时,系统会保留最后发送的一条滞留广播所带信息,当有符合要求的接收器注册时都会自动收到带有该信息的广播
            • StickyBroadcast与StickyOrderedBroadcast的区别同Broadcast与OrderedBroadcast一样
            • 既然能够保存在系统中,那么也就能够删除,删除方法是removeStickyBroadcast
    • 动态注册与静态注册
      • 在Manifest中注册是静态注册,在应用安装后就自动注册到系统中,这种方式长期消耗内存,适合监听系统广播以及应用生命周期中一直使用的广播
      •  Android还支持一种代码中注册的方式,能够灵活实现接收器注册,适合运用在应用内部监听的消息广播
      • 动态注册方式
        • 注册
        • registerReceiver(new MReceiver(),new IntentFilter("com.example.AndroidTest_01.receiver1"));
        • 既然有动态注册那就有对应的动态取消注销
        • MReceiver receiver=new MReceiver();
          unregisterReceiver(receiver);
        • 动态注销需要通过注册时的实例来注销,因此静态注册的接收器不能动态注销,只有动态注册时保存实例引用才能注销
        • 注册与注销配合使用能够优化应用性能,减少不必要的资源消耗
    • 发送局部广播与全局广播
      • 默认的发送广播消息是全局广播,系统中所有符合要求的接收器都能够接收到广播消息
      • 局部广播
        • 通过LocalBroadcastManager来发送局部广播
        • LocalBroadcastManager localBroadcastManager=LocalBroadcastManager.getInstance(MyActivity.this);
          localBroadcastManager.sendBroadcast(new Intent("com.example.AndroidTest_01.receiver1"));
        • 通过LocalBroadcastManager发送的广播能够只在当前应用中传播,不会想其他App发送广播,保证了数据隐私性
        • 通过LocalBroadcastManager注册的接收器也不会接收到其他App发过来的消息,能够防止其他App进行攻击
        • LocalBroadcastManager比全局系统广播更加高效
        • 通常使用局部广播来在应用中进行广播事件纷发与接收,配合动态注销广播的运行能够最大化提高应用效率
  • 相关阅读:
    Advanced Developer's Blog
    图片文字识别
    Unit test resources
    SpringBoot-mvn插件
    flask中使用proto3
    QTA-qtaf自动化测试实践
    AttributeError: module 'virtualenv' has no attribute 'create_environment'
    qtaf dick 报错 NameError: name 'dict_values' is not defined
    24点python实现
    mysql在win下移植
  • 原文地址:https://www.cnblogs.com/xl-xlg/p/5036016.html
Copyright © 2011-2022 走看看