1.activity_case1.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".cast1"><Buttonandroid:id="@+id/cbutton"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginStart="160dp"android:layout_marginLeft="160dp"android:layout_marginTop="16dp"android:text="broadcast"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent" /><TextViewandroid:id="@+id/t"android:layout_width="167dp"android:layout_height="80dp"android:layout_marginStart="160dp"android:layout_marginLeft="160dp"android:layout_marginTop="32dp"android:text="TextView"android:textColor="#000000"android:textSize="30dp"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toBottomOf="@+id/cbutton" />
</androidx.constraintlayout.widget.ConstraintLayout>

case1.java

package com.example.myapplication;import androidx.appcompat.app.AppCompatActivity;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;public class cast1 extends AppCompatActivity {private Button cbutton;private TextView t;private MyBroadCast myBroadCast;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_cast1);cbutton=findViewById(R.id.cbutton);t=findViewById(R.id.t);cbutton.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {Intent intent=new Intent(cast1.this, cast2.class);startActivity(intent);}});myBroadCast=new MyBroadCast();
//        广播的拦截器,接受什么样的广播IntentFilter intentFilter=new IntentFilter();intentFilter.addAction("com.update");LocalBroadcastManager.getInstance(this).registerReceiver(myBroadCast,intentFilter);}//    当activity消失的时候,广播结束,释放资源@Overrideprotected void onDestroy() {super.onDestroy();LocalBroadcastManager.getInstance(this).unregisterReceiver(myBroadCast);}private class MyBroadCast extends BroadcastReceiver{@Overridepublic void onReceive(Context context, Intent intent) {
//            当接收到“com.update”的广播时,设置文本内容为switch (intent.getAction()){case "com.update":t.setText("大家好,这个是广播修改后的数据");}}}
}

2.activity_case2.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".cast2"><Buttonandroid:id="@+id/cbutton"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginStart="112dp"android:layout_marginLeft="112dp"android:layout_marginTop="76dp"android:text="Button"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

case2.java

package com.example.myapplication;import androidx.appcompat.app.AppCompatActivity;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;public class cast2 extends AppCompatActivity {private Button cbutton;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_cast2);cbutton=findViewById(R.id.cbutton);cbutton.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {
//               设置广播的action行为Intent intent=new Intent("com.update");LocalBroadcastManager.getInstance(cast2.this).sendBroadcast(intent);}});}
}

android中的广播机制(动态注册)相关推荐

  1. Android 第十九课 大喇叭--广播机制----动态注册监听网络变化与静态注册实现开机启动

    为了便于进行 系统级别的消息通知,Android引入了一套广播消息机制. 1.广播机制简介: 因为Android中的每个应用程序都可以对自己感兴趣的广播尽心注册,这样程序只会接收自己所关心的广播内容, ...

  2. Android中使用广播机制退出多个Activity

    谷歌百度一下,Android中退出多个Activity的方法,大家讨论的很多. 在实习的时候,看到公司的项目退出多个Activity,是采用LinkedList方法,毕业设计的时候,也参照了那种方法. ...

  3. Android中的广播Broadcast详解

    今天来看一下Android中的广播机制,我们知道广播Broadcast是Android中的四大组件之一,可见他的重要性了,当然它的用途也很大的,比如一些系统的广播:电量低.开机.锁屏等一些操作都会发送 ...

  4. Android学习笔记——广播机制

    Android广播机制 为了便于进行系统级别的消息通知,Android引入了一套广播消息机制. Android中的广播机制十分灵活,每个程序都可以对自己感兴趣的广播进行注册.程序只会接收自己所关心的广 ...

  5. 从信息传递的角度来看Android中的广播和Binder

    缘起 在<关于Android学习的三个终极问题>一文的最后,我曾提到过在看完<信息简史>一书后,顿悟到"开发软件的时候仿佛能看到信息在流动".<信息简 ...

  6. android 增加一条广播,Android中BroadcastReceiver广播使用及注意点

    Android中的广播用途很广,是四大组件之一.在android中可以看到它的各种应用,从系统发出的广播,用户自定义的广播等. 这里详细记录下广播的分类以及使用方法. 广播,是由两方面组成一个流程:广 ...

  7. 【Android】BroadCast广播机制应用与实例

    如何编写广播接收器 第一步:需要继承BroadcastReceiver类,覆写其中的onReceive()方法. class MyBroadcastReceiver extends Broadcast ...

  8. Android中Alarm的机制

    本次给大家分析的是Android中Alarm的机制所用源码为最新的Android4.4.4.首先简单介绍如何使用Alarm并给出其工作原理,接着分析Alarm和Timer以及Handler在完成定时任 ...

  9. 浅谈Android中的MVP与动态代理的结合

    浅谈Android中的MVP与动态代理的结合 本篇文章已授权微信公众号 guolin_blog (郭霖)独家发布 在Android开发平台上接触MVP足足算起来大概已经有一个年头左右.从最开始到现在经 ...

最新文章

  1. 《Photo-Realistic Single Image Super-Resolution Using a Generative Adversarial Network》阅读笔记
  2. JavaScript实现完整的ComplexNumber复数类(附完整源码)
  3. TensorRT(4)-Profiling and 16-bit Inference
  4. xcode于Archive当产生安装包遇到ld: library not found for -lPods
  5. C++17标准制定完成
  6. 【文末有福利】5个字极简入门朴素贝叶斯模型
  7. 虚拟机:请问我刚刚回收的对象是干垃圾还是湿垃圾?|文末送书
  8. python--迭代,生成
  9. 使用Python和Prometheus跟踪天气
  10. 使用虚幻引擎 4 年,网络架构大揭秘
  11. java--迭代(三)foreach解析与字节码
  12. html5中怎么实现外边框中嵌入字_Web前端有什么优点?Web前端怎么入门?
  13. nodejs爬虫获取漫威超级英雄电影海报
  14. 各种绩效考核方法的区别
  15. 计算机spec分值怎么算,四级怎么算分数公式(历年四级常模均值)
  16. linux下文件属性drwxr-xr-x各是什么意思
  17. c语言——数字特征值题
  18. winusb —— 不再为你的usb设备编写驱动
  19. java中使用length获取二维数组的长度
  20. 【百金轻】:油价跌势愈猛 投资者更不应做急单

热门文章

  1. Mac源码安装使用OpenCV
  2. 自定义binder架构的 client/ server组件
  3. Android底层控制系统设置的命令集合
  4. 写给新入职的毕业生们
  5. vscode之调试es6代码
  6. 深度学习自学(三十一):基于变分期望最大化深度学习的非盲噪声图像去模糊
  7. 深度学习自学(二十):SmoothL1 和 Softmax交叉熵
  8. windows 一键安装apache服务器 windows傻瓜式安装apache2 web服务器管理软件
  9. C语言博客作业08,C语言I博客作业08
  10. oracle11g win10版本,win10系统安装的oracle11g和cloud6.2 创建数据中心报错