Android自定义广播接收
作者:Lssの老父亲 时间:2023-04-30 09:39:31
本文实例为大家分享了Android自定义广播接收的具体代码,供大家参考,具体内容如下
实现效果:
MainActivity.java代码:
package com.henu.broadcastsample;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private MyBroadcastReceiver receiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void send(View v) {
//动态注册广播接收
receiver = new MyBroadcastReceiver();
String action = "henurjxy";
IntentFilter intentFilter=new IntentFilter(action);
registerReceiver(receiver,intentFilter);
Intent intend = new Intent("henurjxy");
sendBroadcast(intend);
}
//在页面销毁时,注销广播接收者
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(receiver);
}
}
MyBroadcastReceiver.java代码:
package com.henu.broadcastsample;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
public class MyBroadcastReceiver extends BroadcastReceiver {
public MyBroadcastReceiver(){}
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context,"是我发出的广播哦",Toast.LENGTH_SHORT).show();
}
}
来源:https://blog.csdn.net/qq_50982405/article/details/116718104
标签:Android,广播接收
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
常见Android编译优化问题梳理总结
2021-08-17 11:21:48
![](https://img.aspxhome.com/file/2023/2/137952_0s.png)
C++野指针和悬空指针的实现方法
2022-11-02 08:26:56
Android高仿微信对话列表滑动删除效果
2022-10-29 21:12:43
![](https://img.aspxhome.com/file/2023/0/116010_0s.jpg)
SpringBoot使用token简单鉴权的具体实现方法
2022-07-10 14:23:42
![](https://img.aspxhome.com/file/2023/4/82304_0s.png)
实例分析java对象的序列化和反序列化
2022-07-11 00:22:41
Android自定义ViewGroup实现可滚动的横向布局(2)
2022-10-08 17:25:27
C语言入门篇--初识指针和指针变量
2022-05-31 06:10:45
![](https://img.aspxhome.com/file/2023/3/120803_0s.png)
C#中子类调用父类的实现方法
2023-08-17 09:09:52
StringUtils里的isEmpty方法和isBlank方法的区别详解
2023-07-15 04:29:16
Java 实战练手项目之酒店管理系统的实现流程
2022-07-30 09:05:10
![](https://img.aspxhome.com/file/2023/1/71201_0s.png)
Android实现模仿UCweb菜单效果的方法
2023-08-15 20:22:43
![](https://img.aspxhome.com/file/2023/6/139086_0s.png)
基于Unity制作一个简易的计算器
2023-02-18 10:02:39
![](https://img.aspxhome.com/file/2023/2/82162_0s.png)
Spring中事务传播行为的介绍
2023-06-24 01:21:01
Java设计模式--代理模式
2023-02-26 21:45:32
![](https://img.aspxhome.com/file/2023/5/64885_0s.jpg)
java 中newInstance()方法和new关键字的区别
2023-11-25 07:17:26
C语言实现自定义扫雷游戏(递归版)
2023-11-03 02:12:36
![](https://img.aspxhome.com/file/2023/2/107382_0s.jpg)
Android入门教程之Fragment的具体使用详解
2021-09-30 01:21:57
![](https://img.aspxhome.com/file/2023/8/129008_0s.jpg)
在Android界面上显示和获取Logcat日志输出的方法
2023-02-19 20:02:19
![](https://img.aspxhome.com/file/2023/7/106017_0s.png)
基于Springboot实现JWT认证的示例代码
2023-04-01 09:46:40
![](https://img.aspxhome.com/file/2023/9/83029_0s.jpg)
Android中实现EditText密码显示隐藏的方法
2022-04-13 22:33:56
![](https://img.aspxhome.com/file/2023/6/89506_0s.png)