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. [Shell]MySql慢查询日志GetShell

    通过开启慢查询日志,配置可解析日志文件GETSHELL. MySQL的慢查询日志是MySQL提供的一种日志记录,它用来记录在MySQL中响应时间超过阀值的语句. long_query_time的默认值 ...

  2. Simplifying Failures

    # # Finish the delta debug function ddmin # import re def test(s): print s, len(s),repr(s) if re.sea ...

  3. 深度学习面试题18:网中网结构(Network in Network)

    目录 举例 参考资料 网中网结构通过多个分支的运算(卷积或池化),将分支上的运算结果在深度上连接 举例 一个3*3*2的张量, 与3个1*1*2的卷积核分别same卷积,步长=1, 与2个2*2*2的 ...

  4. 02_01Graph_Session

    import numpy as npimport tensorflow as tfnp.random.seed(42)"""学习:1.图的创建2.tf.constant( ...

  5. 【转】iPhone手机获取uuid 安装测试app

    iPhone手机获取uuid 安装测试app UDID是一种iOS设备的特殊识别码.除序号之外,每台ios装置都另有一组独一无二的号码,我们就称之为识别码( Unique Device Identif ...

  6. 谈谈你对This对象的理解?

    1.this总是指向函数的直接调用者(而非间接调用者):2.如果有new关键字,this指向new出来的那个对象:3.在事件中,this指向触发这个事件的对象,特殊的是,IE中的attachEvent ...

  7. CentOS 7.2 基于Docker实现MySQL主从架构

    原文地址:https://blog.csdn.net/sunnyfg/article/details/80655823 1.安装Docker(略) Centos7下安装Docker : https:/ ...

  8. 模型稳定性指标—PSI

    由于模型是以特定时期的样本所开发的,此模型是否适用于开发样本之外的族群,必须经过稳定性测试才能得知.稳定度指标(population stability index ,PSI)可衡量测试样本及模型开发 ...

  9. layui表格工具条,如何动态控制按钮的展示?

    <script type="text/html" id="toolTpl"> {{# if(d.agrgrtsts == 'A'){ }} < ...

  10. 为什么说基于TCP的移动端IM仍然需要心跳保活?(转)

    源:https://segmentfault.com/a/1190000006832547 为什么说基于TCP的移动端IM仍然需要心跳保活?