<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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"
android:gravity="center"
tools:context=".MainActivity3">
<Button
android:id="@+id/send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="发送广播"></Button>
</LinearLayout>
2.使用广播的第一步当然是创建一个广播接受者
public class MyBrodestReciver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
//判断action是否为添加的action,如果是则toast
String action = intent.getAction();
if (action.equals("one_brodest")){
Toast.makeText(context, "发送了一个广播", Toast.LENGTH_SHORT).show();
}
}
}
3.创建完广播接受者以后注册广播,并且添加一个action
//新建intentFilter对象 通过addAction添加广播
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("one_brodest");
4.然后注册一个广播
//注册广播
MyBrodestReciver myBrodestReciver = new MyBrodestReciver();
registerReceiver(myBrodestReciver,intentFilter);
5.到这里广播的注册已经完成接下来就是使用了
//做一个点击事件发送一个广播
send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction("one_brodest");
sendBroadcast(intent);
}
});
6.这就是点击之后的效果,成功发送了一个广播!!!!!!!!!!!!!!!
7.最后一步,销毁广播
@Override
protected void onDestroy() {
super.onDestroy();
//销毁广播
unregisterReceiver(brodestReciver);
}
最近在和团队规划OKR目标的时候,我们讨论了很多问题,我先抛砖引玉,列举了一些...
Eval 计算一个表达式的值并返回结果。 语法:[result = ]eval_r(expression_r) e...
ajax传送json格式数据,关键是指定contentType,data要是json格式 如果是restful...
今天给大家分享四个在实际开发中,比较实用的SQL Server脚本函数,希望对大家能...
Linux 发行版的爱好者们又有全新的 Linux 可以体验了,日前一款名为 TeLOS 的 Li...
一、导读 本文主要介绍Redhat7.6系统安装oracle 12201版本RAC的一些坑以及避坑方...
如果你想对使用手机,平板电脑和辅助技术的用户隐藏某些内容,而只对键盘用户显...
一. RabbitMQ 简介 MQ全称为Message Queue, 消息队列(MQ)是一种应用程序对应用...
Afrikaans:af Albanian:sq Basque:eu Belarusian:be Bulgarian:bg Catalan:ca Ch...
本文分享自华为云社区《 架构可视化支撑系统演进探索 》原文作者无名小溪。 随着...