網站首頁 編程語言 正文
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
相關推薦
- 2022-07-18 linux引導和計劃任務
- 2023-01-26 Kotlin協程Channel源碼示例淺析_Android
- 2022-09-24 Pytorch實現邏輯回歸分類_python
- 2022-03-16 .Net?Core?多文件打包壓縮的實現代碼_實用技巧
- 2022-06-02 關于docker?registry使用詳解_docker
- 2023-11-11 Python測網絡連通性、能否訪問某個網絡或者端口號<網絡檢測、ping主機、測試端口>
- 2023-07-08 qt報錯***Fault tolerant heap shim applied to current
- 2022-07-13 k8s 之 kubectl 提示 “The connection to the server loc
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支