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+json+php+mysql實現(xiàn)用戶反饋功能方法解析
相信每個項目都會有用戶反饋建議等功能,這個實現(xiàn)的方法很多,下面是我實現(xiàn)的方法,供大家交流2012-11-11
Android?RecyclerChart其它圖表繪制示例詳解
這篇文章主要為大家介紹了Android?RecyclerChart其它圖表繪制示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
java,Android:在eclipse中的快捷鍵(經(jīng)典收藏)
下面的快捷鍵是常用的,本人就本身喜好且常用的收拾一下,現(xiàn)在曬出來與大家分享,感興趣的朋友可以了解小哦2013-01-01
Android LinearLayout實現(xiàn)自動換行
這篇文章主要為大家詳細(xì)介紹了Android LinearLayout實現(xiàn)自動換行,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-08-08
Android Fragment的靜態(tài)注冊和動態(tài)注冊創(chuàng)建步驟
這篇文章主要介紹了Android Fragment的靜態(tài)注冊和動態(tài)注冊創(chuàng)建步驟,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2020-02-02

