1.介绍

2.实现方法

3.注册广播

(1)静态广播

在AndroidManifest.xml文件中注册广播

<intent-filter>为过滤器
<receiver android:name=".MyBroadCast1"></receiver>
<receiver android:name=".MyBroadCast2">
<intent-filter>
<action android:name="com.lucky"></action>
</intent-filter>
</receiver>

(2)动态广播

4.静态广播代码

(1)主界面

package com.lucky.test40broadcastreceiver;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button; public class MainActivity extends AppCompatActivity {
Button button1;
Button button2; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1=findViewById(R.id.button);
button2=findViewById(R.id.button2);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this,MyBroadCast1.class);
sendBroadcast(intent); //发送广播
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//设置Action为com.lucky,会在AndroidManifest.xml中的receiver中启动action为com.lucky的广播
Intent intent=new Intent();
intent.setAction("com.lucky");
sendBroadcast(intent);
}
});
}
}

(2)广播接收器1(采用Toast语句进行提示)

package com.lucky.test40broadcastreceiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast; //MyBroadCast1广播接收器
public class MyBroadCast1 extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context,"接收到广播",Toast.LENGTH_SHORT).show();
}
}

(3)广播接收器2(采用通知的方式进行提示)

package com.lucky.test40broadcastreceiver;

import android.app.Notification;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.support.annotation.RequiresApi; public class MyBroadCast2 extends BroadcastReceiver {
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onReceive(Context context, Intent intent) {
//采用通知的形式
NotificationManager manager= (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification.Builder builder=new Notification.Builder(context);
builder.setSmallIcon(R.mipmap.ic_launcher); //给通知设置图标
builder.setTicker("广播消息"); //设置提示消息
builder.setContentTitle("您有一条广播消息");//设置内容标题
builder.setContentText("hello lucky"); //设置通知内容
manager.notify(0x01,builder.build()); //让通知显示
}
}

5.动态广播

(1)主界面

package com.lucky.test41broadcastreceiver2;

import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button; public class MainActivity extends AppCompatActivity {
Button button1;
MyBroadCast myBroadCast; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1=findViewById(R.id.button); //注册广播
myBroadCast=new MyBroadCast(); //实例化广播接收器
IntentFilter intentFilter=new IntentFilter();//实例化一个过滤器
intentFilter.addAction("com.lucky"); //添加action来明确接收哪种类型的广播
registerReceiver(myBroadCast,intentFilter);//注册广播 button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//发送广播
Intent intent=new Intent();
intent.setAction("com.lucky");
sendBroadcast(intent);
}
});
}
}

(2)广播接收器

package com.lucky.test41broadcastreceiver2;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast; public class MyBroadCast extends BroadcastReceiver { @Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context,"hello lucky",Toast.LENGTH_SHORT).show();
}
}

035 Android 广播(BroadCastReceiver)的更多相关文章

  1. Android 广播 BroadcastReceiver

    Android 系统里定义了各种各样的广播,如电池的使用状态,电话的接收和短信的接收,开机启动都会产生一个广播.当然用户也可以自定义自己的广播. 既然说到广播,那么必定有一个广播发送者,以及广播接收器 ...

  2. Android广播BroadcastReceiver 二

    BroadcastReceiver: 在Android中,Broadcast是一种广泛运用的在应用程序之间传输信息的机制.而BroadcastReceiver是对发送出来的 Broadcast进行过滤 ...

  3. Android广播BroadcastReceiver 一

    Android 系统里定义了各种各样的广播,如电池的使用状态,电话的接收和短信的接收,开机启动都会产生一个广播.当然用户也可以自定义自己的广播. 既然说到广播,那么必定有一个广播发送者,以及广播接收器 ...

  4. Android广播BroadcastReceiver

    Android 系统里定义了各种各样的广播,如电池的使用状态,电话的接收和短信的接收,开机启动都会产生一个广播.当然用户也可以自定义自己的广播. 既然说到广播,那么必定有一个广播发送者,以及广播接收器 ...

  5. Android中BroadcastReceiver广播

    BroadCastReceiver 简介 广播接收者( BroadcastReceiver )用于接收广播 Intent ,广播 Intent 的发送是通过调用 Context.sendBroadca ...

  6. Android - 广播接收者 - BroadcastReceiver

    BroadcastReceiver 介绍: 广播是一种广泛运用的在应用程序之间传输信息的机制 .而 BroadcastReceiver 是对发送出来的广播 进行过滤接收并响应的一类组件 接受一种或者多 ...

  7. Android笔记(二十六) Android中的广播——BroadcastReceiver

    为了方便进行系统级别的消息通知,Android有一套类似广播的消息机制,每个应用程序都可以对自己感兴趣的广播进行注册,这样该程序就只会接收自己所关心的广播内容,这些广播可能是来自于系统,也可能是来自于 ...

  8. Android(java)学习笔记172:BroadcastReceiver之 Android广播机制

    Android广播机制 android系统中有各式各样的广播,各种广播在Android系统中运行,当"系统/应用"程序运行时便会向Android注册各种广播.Android接收到广 ...

  9. (八)Android广播接收器BroadcastReceiver

    一.使用Broadcast Reciver 1.右击java文件夹,new->other->Broadcast Receiver后会在AndroidManifest.xml文件中生成一个r ...

随机推荐

  1. Linux之字符串截取

    获取字符串的长度 在 Shell 中获取字符串长度很简单,具体方法如下: ${#string_name} string_name 表示字符串名字. root@master:~# b="ma ...

  2. [代码审计]PHP_Bugs题目总结(1)

    0x00 简介 最近这几天看到了许多关于代码审计的ctf题,在电脑里也翻出来好长时间没看过的php_bugs,干脆最近把这个好好看看! 下载地址:https://github.com/bowu678/ ...

  3. 使用 WebRTC 构建简单的前端视频通讯

    在传统的 Web 应用中,浏览器与浏览器之间是无法直接相互通信的,必须借助服务器的帮助,但是随着 WebRTC 在各大浏览器中的普及,这一现状得到了改变. WebRTC(Web Real-Time C ...

  4. 2019-2020-1 20175313 《信息安全系统设计基础》实现mypwd

    目录 MyPWD 一.题目要求 二.题目理解 三.需求分析 四.设计思路 五.伪代码分析 六.码云链接 七.运行结果截图 MyPWD 一.题目要求 学习pwd命令 研究pwd实现需要的系统调用(man ...

  5. Ubuntu 18.04.1 安装mysql 5.7.27

    sudo apt-get update sudo apt-get install mysql-server 配置 sudo mysql_secure_installation 参考文档 Ubuntu1 ...

  6. Python操作excel工具

    python操作excel的工具类有很多,下面举几个常见的工具类: 一. 1.xlrd 只能读取excel操作,支持xls和xlsx两种格式的 2.xlwt 只能写入excel操作,只支持 xls格式 ...

  7. Oracle的存储的三大物理文件

      分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! 一. ...

  8. * resolve_conffiles: Existing conffile /etc/config/dhcp is different from the conffile in the new package. The new conffile will be placed at /etc/config/dhcp-opkg.

    * resolve_conffiles: Existing conffile /etc/config/dhcp is different from the conffile in the new pa ...

  9. NTC3950-10K温度传感器

    一.计算公式 补充: B=3950 R=10K T2=25度 查RT表,25度对应的是10K 电路: 热敏电阻与上拉电阻R813分压,获取温度与Vo电压的关系,在根据Vo折算出与MCU ADC的数值. ...

  10. css 文本省略号显示

    文本省略号是非常常见的需求,而省略号展示又通常分为俩种情况折行和不折行.不折行: div { white-space:nowrap;/* 规定文本是否折行 */ overflow: hidden;/* ...