網(wǎng)站首頁 編程語言 正文
BroadcastReceiver靜態(tài)注冊案例演示,供大家參考,具體內(nèi)容如下
靜態(tài)注冊與動態(tài)注冊的區(qū)別:
動態(tài)注冊:廣播接收器可以自由的控制注冊與取消,具有很大的靈活性。但只有在應(yīng)用程序啟動后才能收到廣播。并且動態(tài)注冊的廣播接收器的生命周期與其對應(yīng)的Acitivity的生命周期是一致的。
靜態(tài)注冊:又叫做清單注冊,即在AndroidManifest.xml中進(jìn)行注冊。靜態(tài)注冊的廣播不受程序是否啟動的約束,當(dāng)應(yīng)用程序關(guān)閉后,還可以接收到廣播。
效果圖:
代碼:
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.指定組件名,依據(jù)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="發(fā)送自定義廣播"/> </LinearLayout>
原文鏈接:https://blog.csdn.net/weixin_42456748/article/details/124693862
相關(guān)推薦
- 2022-12-27 刪除Helm使用時(shí)關(guān)于kubernetes文件的警告問題_云其它
- 2022-08-19 React中Props的使用
- 2022-11-07 C++?線段樹原理與實(shí)現(xiàn)示例詳解_C 語言
- 2022-09-13 一文帶你了解Go語言中的單元測試_Golang
- 2022-03-20 android自定義對話框?qū)嵗a_Android
- 2022-04-09 SpringMVC 基礎(chǔ)配置文件(簡潔版本)
- 2022-05-02 Redis可視化連接服務(wù)器的方法_Redis
- 2022-03-28 Python?NumPy實(shí)用函數(shù)筆記之a(chǎn)llclose_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支