BroadcastReceiver靜態(tài)注冊案例詳解
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>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android實(shí)現(xiàn)從緩存中讀取圖片與異步加載功能類
這篇文章主要介紹了Android實(shí)現(xiàn)從緩存中讀取圖片與異步加載功能類,涉及Android針對緩存的操作及圖片異步加載相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-08-08Android輕量級存儲SharedPreferences?MMKV?Jetpack?DataStore方案
這篇文章主要為大家介紹了Android輕量級存儲SharedPreferences?MMKV?Jetpack?DataStore方案示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08淺析Android中常見三種彈框在項(xiàng)目中的應(yīng)用
這篇文章主要介紹了淺析Android中常見三種彈框在項(xiàng)目中的應(yīng)用,需要的朋友可以參考下2017-03-03Ubuntu中為Android系統(tǒng)上編寫Linux內(nèi)核驅(qū)動程序?qū)崿F(xiàn)方法
本文主要介紹在Ubuntu 上為Android系統(tǒng)編寫Linux內(nèi)核驅(qū)動程序, 這里對編寫驅(qū)動程序做了詳細(xì)的說明,對研究Android源碼和HAL都有巨大的幫助,有需要的小伙伴可以參考下2016-08-08Android實(shí)現(xiàn)計(jì)時與倒計(jì)時的常用方法小結(jié)
這篇文章主要介紹了Android實(shí)現(xiàn)計(jì)時與倒計(jì)時的常用方法,總結(jié)并對比分析了幾種常用計(jì)時方法的特點(diǎn),具有一定參考借鑒價值,需要的朋友可以參考下2015-10-10ScrollView與SeekBar綁定實(shí)現(xiàn)滑動時出現(xiàn)小滑塊效果
這篇文章主要為大家詳細(xì)介紹了ScrollView與SeekBar綁定實(shí)現(xiàn)滑動時出現(xiàn)小滑塊效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-10-10Android RatingBar星星評分控件實(shí)例代碼
本文通過實(shí)例代碼給大家介紹了Android RatingBar星星評分控件,非常不錯,具有參考借鑒價值,需要的朋友參考下吧2017-06-06