zoukankan      html  css  js  c++  java
  • Android BroadcastReceiver广播接受者


    静态注册
    配置清单表注册:只要曾经注册过哪怕关闭也能调用
     方式一:sendBroadCastReceive
      广播的步骤:
          发送  无序广播,普通广播
          (1).发送方
             Intent intent=new Intent();
             intent.setAction("");发送的内容
            sendBroadcast(intent);
          (2).接受方
          创建一个reserver类继承BroadcastReceiver
            重写receiver方法
            配置receiver并且写receiver的属性intent-filter,--action
            注:action的名字和发送的内容相同setAction;
           
           (3). 主要作用service方法里面执行,可以在不同应用之间进行通信
          
           注:如果多个action一样,先执行当前项目里面的然后在执行其他项目中的;即接受方同时收到广播没有优先级别
     
    方式二:
      发送有序广播
             1.创建
                Intent intent=new Intent("youxu");
                sendOrderedBroadcast(intent, null, null, null, 1, "我说了算", null);
             2.创建broadReciver
                getResultData();获取发送的信息
                setResultData();修改发送的信息
                abortBroadcast();拦截发送的信息,终止发送;
             3.配置
             <receiver android:name="com.example.reseiver.TwoBroadReceive">
     
                <intent-filter android:priority="900">
                    <action android:name="youxu"/>
     
                </intent-filter>
            </receiver>
            priority为-1000到1000数值越大优先级越高
     
     
     
             短信的接受加权限设置的波段action
              <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
             权限 android.permission.RECEIVE_SMS
            
            
            
             开机启动的action即从非Activity启动
             <action android:name="android.intent.action.BOOT_COMPLETED"/>
             开机启动需要调到activity必须设置flags
             intent.setFlags(Intent.FLAG_ACTIVITYw_NEW_TASK);
               </receiver>
              <receiver android:name="com.example.duanxin.KaiJiXinResever"            >
                <intent-filter >
                    <action android:name="android.intent.action.BOOT_COMPLETED"/>
                    </intent-filter>
            </receiver>ww
     
    动态注册:
        在当前Acitivity中配置
    在onStart中注册,onStop中取消unregisterReceiver
                   DongTaiBroadReceive receive;
                   IntentFilter intentfilter=new IntentFilter("intent");
                   receive = new DongTaiBroadReceive();
                   registerReceiver(receive, intentfilter);
                   Intent inten=new Intent("intent");
                   sendBroadcast(inten);
     
      在当前Acitivity中取消receiver
      protected void onDestroy() {
            // TODO Auto-generated method stub
     
             unregisterReceiver(receive);
            super.onDestroy();
        }
     
     
     
  • 相关阅读:
    用fiddler测试ip轮询
    ubuntu下安装fiddler
    Andriod相机开发关于startPreview Failed的错误的特别记录(重要)
    我的Cocos2dx开发模式
    Android WebView导入HTML使Js生效的方法
    Lua快捷键
    String,StringBuilder,StringBuffer的对比测试
    重构视角(摘抄)
    String属于“假引用类型”,代码为证(一个String引发的血案...)
    static class
  • 原文地址:https://www.cnblogs.com/huihuizhang/p/5216267.html
Copyright © 2011-2022 走看看