这篇文章主要为大家详细介绍了BroadcastReceiver静态注册案例,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
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>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程学习网。
沃梦达教程
本文标题为:BroadcastReceiver静态注册案例详解
基础教程推荐
猜你喜欢
- MVVMLight项目Model View结构及全局视图模型注入器 2023-05-07
- iOS中如何判断当前网络环境是2G/3G/4G/5G/WiFi 2023-06-18
- Android开发Compose集成高德地图实例 2023-06-15
- Android实现短信验证码输入框 2023-04-29
- IOS获取系统相册中照片的示例代码 2023-01-03
- iOS开发使用XML解析网络数据 2022-11-12
- iOS Crash常规跟踪方法及Bugly集成运用详细介绍 2023-01-18
- Flutter进阶之实现动画效果(三) 2022-10-28
- iOS开发 全机型适配解决方法 2023-01-14
- Android Compose自定义TextField实现自定义的输入框 2023-05-13