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 ...
随机推荐
- Flask一种通用视图,增删改查RESTful API的设计
模型设计是后端开发的第一步.数据模型反映了各种对象之间的相互关系. from app import db class Role(db.Model): """角色" ...
- Pytest权威教程20-日志
目录 记录日志 caplog Fixture方法 实时日志 版本改动记录 Pytest3.4中不向后兼容的更改 返回: Pytest权威教程 记录日志 Pytest默认捕获WARNING以上日志消息, ...
- localstorage 必知必会
做项目中发现localstorage在不同的域名下是不能相互访问的,于是找到了以下这篇文章,对localStorage做一个深入的了解 HTML API localstorage在浏览器的API有两个 ...
- 生成模型 VS 判别模型 (含义、区别、对应经典算法)
从概率分布的角度考虑,对于一堆样本数据,每个均有特征Xi对应分类标记yi. 生成模型:学习得到联合概率分布P(x,y),即特征x和标记y共同出现的概率,然后求条件概率分布.能够学习到数据生成的机制 ...
- JS 读取 获取 cookie
alert(document.cookie); cookie 只能获取当前域名的cookie, 该页面的其他域名的获取不了的.
- qt 应用程序版本设置方法
pro 增加 VERSION = 1.2.3.4 DEFINES += APP_VERSION=\\\"$$VERSION\\\" 应用程序中用 APP_VERSION 宏就可以获 ...
- 修改layui的表单手机、邮箱验证可以为空怎么实现?
修改layui的表单手机.邮箱验证可以为空 解决办法: 修改源码: 把表单验证源代码(form.js)的正则表达式改一下,例如手机的正则为:/^1d{10}$/,可以改成/^$|^1d{10} ...
- cv2.warpAffine 参数详解
本文链接:https://blog.csdn.net/qq878594585/article/details/81838260本文为作者原创文章,未经同意严禁转载! opencv中的仿射变换在pyth ...
- https://pingcap.com/blog-cn/flame-graph/
https://pingcap.com/blog-cn/flame-graph/ 因为 TiKV 是自己内部使用了 jemalloc,并没有用系统的 malloc,所以我们不能直接用 perf 来探查 ...
- GIS地理工具案例教程——合并选中图层
GIS地理工具案例教程--合并选中图层 商务合作,科技咨询,版权转让:向日葵,135-4855__4328,xiexiaokui#qq.com 功能:并图层列表中 描述:对图层列表中选中图层进行合并. ...