zoukankan      html  css  js  c++  java
  • Broadcast发送广播

    一、知识介绍

      1、【广播分类】

        ①有序广播:接收者A收到广播传递给B,B传给C,有序传递。任何一个环节都可以终止广播,也可以修改广播中携带的数据。

          发送的方式:sendOrderedBroadcast(intent,receiverPermission);

          【提示】①第二个参数是设置发送的权限,这里可以设为null

             ②接收有序广播是需要在intent-flter中设置priority,值越大则先执行,相同则按照注册顺序

        ②无序广播:一个广播发送者,向所有接收者同时发送广播,也就是ABC接收者都同时响应。

          发送方式:sendBroadcast(intent)

      2、【广播接收者】按是否常驻分类

        ①常驻型广播接收者:在androidManifest.xml中注册,只要应用程序没有被卸载就持续存在。

        ②非常驻型广播接收者:在java代码中注册,一般随Activity或者Service组件产生而产生,随他们销毁而销毁。生命周期比较短。使用的方法是registerReceiver(参数1:广播接收者实例,参数2:频道(意图过滤器));unregisterReceiver(广播接收者实例)

        

    二、项目一【发送广播】

    【步骤】

      ①定义一个广播接收者,自定义添加intent-fliter中的action name

      ②添加按钮,点击事件

      ③定义intent,设置action,发送广播

    【项目结构】

         

    【MyReceiver】  

     1 import android.content.BroadcastReceiver;
     2 import android.content.Context;
     3 import android.content.Intent;
     4 import android.widget.Toast;
     5 
     6 public class MyReceiver extends BroadcastReceiver {
     7 
     8     @Override
     9     public void onReceive(Context context, Intent intent) {
    10         // TODO: This method is called when the BroadcastReceiver is receiving
    11         Toast.makeText(context, "收到广播", Toast.LENGTH_SHORT).show();
    12     }
    13 }

    【AndroidManifest.xml】

    1 <receiver
    2             android:name=".receiver.MyReceiver"
    3             android:enabled="true"
    4             android:exported="true">
    5             <intent-filter>
    6                 <action android:name="com.example.MyApplication2.myreceiver" />
    7             </intent-filter>
    8         </receiver>

    【activity_main.xml】

    1     <Button
    2        android:id="@+id/btn"
    3        android:text="发送广播"
    4        android:layout_width="match_parent"
    5        android:layout_height="wrap_content" />

    【MainActivity】

     1 import android.content.Intent;
     2 import android.support.v7.app.AppCompatActivity;
     3 import android.os.Bundle;
     4 import android.view.View;
     5 import android.widget.Button;
     6 
     7 public class MainActivity extends AppCompatActivity {
     8 
     9     Button btn;
    10     @Override
    11     protected void onCreate(Bundle savedInstanceState) {
    12         super.onCreate(savedInstanceState);
    13         setContentView(R.layout.activity_main);
    14 
    15         btn = findViewById(R.id.btn);
    16         btn.setOnClickListener(new View.OnClickListener() {
    17             @Override
    18             public void onClick(View view) {
    19                 Intent intent = new Intent("com.example.MyApplication2.myreceiver");
    20                 sendBroadcast(intent);
    21             }
    22         });
    23         
    24     }
    25 }

    【提示】发送广播intent设置的action要和广播接受者设置的action相同,这样广播接收者才能收到发送的广播

    【效果】点击

        

     

     二、项目二【发送有序广播】

    【步骤】

      ①定义三个广播接收者,观察顺序

      ②添加按钮点击

      ③设置intent,发送有序广播

    【项目结构】

        

    【定义三个广播接收者并注册】

     1 <receiver
     2             android:name=".receiver.MyOrderReceiver1"
     3             android:enabled="true"
     4             android:exported="true">
     5             <intent-filter android:priority="1000">
     6                 <action android:name="com.example.MyApplication2.myreceiver" />
     7             </intent-filter>
     8         </receiver>
     9         <receiver
    10             android:name=".receiver.MyOrderReceiver2"
    11             android:enabled="true"
    12             android:exported="true">
    13             <intent-filter android:priority="100">
    14                 <action android:name="com.example.MyApplication2.myreceiver" />
    15             </intent-filter>
    16         </receiver>
    17         <receiver
    18             android:name=".receiver.MyOrderReceiver3"
    19             android:enabled="true"
    20             android:exported="true">
    21             <intent-filter android:priority="10">
    22                 <action android:name="com.example.MyApplication2.myreceiver" />
    23             </intent-filter>
    24         </receiver>

    【提示】设置priority为不同的值,action name为相同的,接收同一个广播

        

        

        

    【MainActivity】

    1         btn2 = findViewById(R.id.btn2);
    2         btn2.setOnClickListener(new View.OnClickListener() {
    3             @Override
    4             public void onClick(View view) {
    5                 Intent intent = new Intent("com.example.MyApplication2.myreceiver");
    6                 sendOrderedBroadcast(intent,null);
    7             }
    8         });

    【效果】

      点击按钮执行顺序

     

     

     

  • 相关阅读:
    微信公众号 发送客服消息
    juqery 点击谁获取他的值,赋给input标签
    微信执行退出页面,直接回到微信对话窗口
    微信jssdk上传图片,一张一张的上传 和 一次性传好几张
    juqery 判断所有input 不能为空 判断只能为数字 判断身份证号:18位和15位 判断是否银行卡号
    php foreach
    现在越来越喜欢用ajax传值了,这样能让网站的体验性很好,今天就总结了一下常用的
    有时候不用explode截取字符串了,可以用用substr()
    ztree 文件夹类型的 树状图
    POJ 1065 Wooden Sticks
  • 原文地址:https://www.cnblogs.com/xqz0618/p/eendbroadcast.html
Copyright © 2011-2022 走看看