BroadcastReceiver静态注册案例详解
作者:砖厂打工仔 时间:2022-12-30 18:31:10
BroadcastReceiver静态注册案例演示,供大家参考,具体内容如下
静态注册与动态注册的区别:
动态注册:广播 * 可以自由的控制注册与取消,具有很大的灵活性。但只有在应用程序启动后才能收到广播。并且动态注册的广播 * 的生命周期与其对应的Acitivity的生命周期是一致的。
静态注册:又叫做清单注册,即在AndroidManifest.xml中进行注册。静态注册的广播不受程序是否启动的约束,当应用程序关闭后,还可以接收到广播。
效果图:
代码:
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Intent intent = new Intent("com.example.action.MY_BROADCAST");
//sendBroadcast(intent);
//Android 8.0后不再支持以上这种方式
//需采用指定包名或组件名的形式,即显式Intent
//1.指定包名
//Intent intent = new Intent();
//intent.setAction("com.example.action.MY_BROADCAST");
//intent.setPackage("com.example.a02staticregister");
//sendBroadcast(intent);
//2.指定组件名,依据intent的Component属性
Intent intent = new Intent();
intent.setComponent(new ComponentName(MainActivity.this,StaticReceiver.class));
//也可简写成以下形式,常用写法
//Intent intent = new Intent(MainActivity.this,StaticReceiver.class);
sendBroadcast(intent);
}
});
}
}
StaticReceiver.java
public class StaticReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context,"StaticReceiver收到广播了~~",Toast.LENGTH_LONG).show();
}
}
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.a02staticregister">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.BroadcastRecetiverTest">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".StaticReceiver"
android:exported="true">
<intent-filter>
<action android:name="com.example.action.MY_BROADCAST"/>
</intent-filter>
</receiver>
</application>
</manifest>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="发送自定义广播"/>
</LinearLayout>
来源:https://blog.csdn.net/weixin_42456748/article/details/124693862
标签:BroadcastReceiver,静态注册
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android实现状态栏(statusbar)渐变效果的示例
2023-09-19 19:13:43
![](https://img.aspxhome.com/file/2023/1/138981_0s.png)
如何使用Android注解处理器
2023-08-16 16:49:05
![](https://img.aspxhome.com/file/2023/8/139418_0s.jpg)
C# this关键字的四种用法
2022-10-06 07:09:22
![](https://img.aspxhome.com/file/2023/5/75595_0s.jpg)
C#方法中参数ref和out详解
2021-07-19 12:59:32
![](https://img.aspxhome.com/file/2023/7/106387_0s.png)
Java中final与继承操作实例分析
2023-09-14 08:56:02
![](https://img.aspxhome.com/file/2023/4/114044_0s.png)
SpringBoot 如何实时刷新静态文件
2023-11-02 13:27:09
![](https://img.aspxhome.com/file/2023/5/58765_0s.png)
spring boot 2整合swagger-ui过程解析
2021-08-08 22:57:35
![](https://img.aspxhome.com/file/2023/1/76481_0s.png)
Android添加水印的正确方法 只要三步!
2022-08-07 16:48:15
![](https://img.aspxhome.com/file/2023/9/130419_0s.jpg)
SpringBoot 使用 FTP 操作文件的过程(删除、上传、下载文件)
2021-07-26 10:40:05
C#通过NPOI操作Excel的实例代码
2022-01-20 17:26:29
![](https://img.aspxhome.com/file/2023/1/119601_0s.png)
android 自定义控件 自定义属性详细介绍
2022-08-05 12:19:41
Flutter深色模式适配的实现
2023-01-22 12:41:03
![](https://img.aspxhome.com/file/2023/0/138710_0s.jpg)
C#读取二进制文件方法分析
2022-04-23 11:17:43
基于FeignException$InternalServerError的解决方案
2023-04-25 15:50:45
![](https://img.aspxhome.com/file/2023/2/60492_0s.png)
SpringCloud读取Nacos配置中心报错及遇到的坑:Could not resolve placeholder ‘xxx’ in value ‘${xxx}
2022-12-26 04:41:02
![](https://img.aspxhome.com/file/2023/5/129225_0s.png)
详解Android Activity之间跳转出现短暂黑屏的处理方法
2021-06-28 04:44:03
C#基于简单工厂模式实现的计算器功能示例
2023-06-19 12:40:10
详解Spring Boot读取配置文件与配置文件优先级
2022-03-23 13:14:23
intellij idea14打包apk文件和查看sha1值
2022-05-25 13:18:37
![](https://img.aspxhome.com/file/2023/6/91746_0s.jpg)
深入了解Java核心类库--BigDecimal和System类
2023-12-18 01:50:28