035 Android 广播(BroadCastReceiver)
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)的更多相关文章
- Android 广播 BroadcastReceiver
Android 系统里定义了各种各样的广播,如电池的使用状态,电话的接收和短信的接收,开机启动都会产生一个广播.当然用户也可以自定义自己的广播. 既然说到广播,那么必定有一个广播发送者,以及广播接收器 ...
- Android广播BroadcastReceiver 二
BroadcastReceiver: 在Android中,Broadcast是一种广泛运用的在应用程序之间传输信息的机制.而BroadcastReceiver是对发送出来的 Broadcast进行过滤 ...
- Android广播BroadcastReceiver 一
Android 系统里定义了各种各样的广播,如电池的使用状态,电话的接收和短信的接收,开机启动都会产生一个广播.当然用户也可以自定义自己的广播. 既然说到广播,那么必定有一个广播发送者,以及广播接收器 ...
- Android广播BroadcastReceiver
Android 系统里定义了各种各样的广播,如电池的使用状态,电话的接收和短信的接收,开机启动都会产生一个广播.当然用户也可以自定义自己的广播. 既然说到广播,那么必定有一个广播发送者,以及广播接收器 ...
- Android中BroadcastReceiver广播
BroadCastReceiver 简介 广播接收者( BroadcastReceiver )用于接收广播 Intent ,广播 Intent 的发送是通过调用 Context.sendBroadca ...
- Android - 广播接收者 - BroadcastReceiver
BroadcastReceiver 介绍: 广播是一种广泛运用的在应用程序之间传输信息的机制 .而 BroadcastReceiver 是对发送出来的广播 进行过滤接收并响应的一类组件 接受一种或者多 ...
- Android笔记(二十六) Android中的广播——BroadcastReceiver
为了方便进行系统级别的消息通知,Android有一套类似广播的消息机制,每个应用程序都可以对自己感兴趣的广播进行注册,这样该程序就只会接收自己所关心的广播内容,这些广播可能是来自于系统,也可能是来自于 ...
- Android(java)学习笔记172:BroadcastReceiver之 Android广播机制
Android广播机制 android系统中有各式各样的广播,各种广播在Android系统中运行,当"系统/应用"程序运行时便会向Android注册各种广播.Android接收到广 ...
- (八)Android广播接收器BroadcastReceiver
一.使用Broadcast Reciver 1.右击java文件夹,new->other->Broadcast Receiver后会在AndroidManifest.xml文件中生成一个r ...
随机推荐
- nodejs新工具-cypress和testcofe的崛起
今天咨询一个自动化 工具问题,偶然间有人提起了这个可能以后会很火的工具,在此找到一篇很好的参考文章 记录并为以后做准备 cypress和testcofe https://www.jianshu.com ...
- Android中进度条
<ProgressBar android:id="@+id/progress_bar" android:layout_width="match_parent&quo ...
- Java 学习之路(1)第一个Java程序
Hello World程序 在编程语言的世界里,第一个编程语言估计就是输出Hello World了吧. /** * 编写第一个Java程序,输出Hello World! * @author LJS * ...
- 怎么样在vue-cli的项目里面引入element ui
第一步:先进入到项目里面 npm i element-ui -S 第二步: import Vue from 'vue'; import ElementUI from 'element-ui'; //这 ...
- 乌班图下fluent开启并行报错的解决方法
参考链接: CFD-online原帖:http://www.cfd-online.com/Forums/fluent/149668-fluent-16-0-0-ubuntu-12-04-a.html ...
- 2019软工实践_Alpha(2/6)
队名:955 组长博客:https://www.cnblogs.com/cclong/p/11862633.html 作业博客:https://edu.cnblogs.com/campus/fzu/S ...
- Java利用FastJson一行代码转List<Map>为List<Bean>
/** * 字符串 -> json对象.实体对象.Map.List.List<Map> */ // 字符串 -> json对象 JSONObject json = JSON.p ...
- JSON HiJacking攻击
JSON劫持类似于CSRF攻击,为了了解这种攻击方式,我们先看一下Web开发中一种常用的跨域获取数据的方式:JSONP. 先说一下JSON吧,JSON是一种数据格式,主要由字典(键值对)和列表两种存在 ...
- 第2课第3节_Java面向对象编程_继承性_P【学习笔记】
摘要:韦东山android视频学习笔记 面向对象程序的三大特性之继承性:继承性的主要作用就是复用代码.继承性也有一定的限制,如图一 图一 1.我们在第2课第2节_Java面向对象编程_封装性_P 中 ...
- 新概念英语第二册Lesson5:No wrong numbers
Lesson 5 No wrong numbers 无错号之虞 First listen and then answer the question. 听录音,然后回答以下问题. What does ' ...