BroadcastReceiver動態(tài)注冊案例詳解
BroadcastReceiver動態(tài)注冊案例演示,供大家參考,具體內(nèi)容如下
此案例共介紹2種動態(tài)注冊廣播接收器,為自定義廣播接收器和系統(tǒng)廣播接收器。當點擊發(fā)送按鈕后,將會彈出收到自定義廣播的提示;當打開或關(guān)閉飛行模式時,會出現(xiàn)飛行模式發(fā)生變化的提示。
效果圖:
代碼:
MainActivity.java
public class MainActivity extends AppCompatActivity { ? ? private MyBroadcastReceiver receiver1; ? ? private NetReceiver receiver2; ? ? @Override ? ? protected void onCreate(Bundle savedInstanceState) { ? ? ? ? super.onCreate(savedInstanceState); ? ? ? ? setContentView(R.layout.activity_main); ? ? ? ? //1.動態(tài)注冊自定義廣播接收器 ? ? ? ? receiver1 = new MyBroadcastReceiver(); ? ? ? ? IntentFilter filter1 = new IntentFilter(); ? ? ? ? //使用包名作為自定義廣播action ? ? ? ? filter1.addAction("com.example.a01dynamicregister"); ? ? ? ? registerReceiver(receiver1, filter1); ? ? ? ? //2.動態(tài)注冊系統(tǒng)廣播接收器 ? ? ? ? receiver2 = new NetReceiver(); ? ? ? ? IntentFilter filter2 = new IntentFilter(); ? ? ? ? //添加切換飛行模式action ? ? ? ? filter2.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED);//當飛行模式打開或關(guān)閉時,接收該廣播 ? ? ? ? registerReceiver(receiver2, filter2); ? ? ? ? //3.點擊按鈕發(fā)送自定義廣播 ? ? ? ? findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() { ? ? ? ? ? ? @Override ? ? ? ? ? ? public void onClick(View view) { ? ? ? ? ? ? ? ? Intent intent = new Intent("com.example.a01dynamicregister");//注意:自定義廣播的發(fā)送和接受內(nèi)容必須一直,否則接收不到 ? ? ? ? ? ? ? ? sendBroadcast(intent); ? ? ? ? ? ? } ? ? ? ? }); ? ? } ? ? @Override ? ? protected void onDestroy() { ? ? ? ? super.onDestroy(); ? ? ? ? //記得用完需要銷毀廣播接收器 ? ? ? ? if (receiver1 != null) { ? ? ? ? ? ? unregisterReceiver(receiver1); ? ? ? ? } ? ? ? ? if (receiver2 != null) { ? ? ? ? ? ? unregisterReceiver(receiver2); ? ? ? ? } ? ? } } //接收自定義廣播 class MyBroadcastReceiver extends BroadcastReceiver { ? ? @Override ? ? public void onReceive(Context context, Intent intent) { ? ? ? ? String action = intent.getAction(); ? ? ? ? Toast.makeText(context, "收到自定義廣播了~~action為:" + action, Toast.LENGTH_LONG).show(); ? ? } } //接收系統(tǒng)廣播 class NetReceiver extends BroadcastReceiver { ? ? @Override ? ? public void onReceive(Context context, Intent intent) { ? ? ? ? String action = intent.getAction(); ? ? ? ? if (Intent.ACTION_AIRPLANE_MODE_CHANGED.equals(action)) { ? ? ? ? ? ? Toast.makeText(context, "飛行模式發(fā)生變化~~", Toast.LENGTH_LONG).show(); ? ? ? ? } ? ? } }
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)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
解決Android Studio 3.0 butterknife:7.0.1配置的問題
下面小編就為大家分享一篇解決Android Studio 3.0 butterknife:7.0.1配置的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2017-12-12Android getBackground().setAlpha遇到問題解決辦法
這篇文章主要介紹了Android getBackground().setAlpha遇到問題解決辦法的相關(guān)資料用,getBackground().setAlpha,導致其他布局背景透明度都改變的問題,需要的朋友可以參考下2017-03-03Android編程實現(xiàn)popupwindow定時消失的方法
這篇文章主要介紹了Android編程實現(xiàn)popupwindow定時消失的方法,結(jié)合實例形式分析了Android使用定時器實現(xiàn)popupwindow定時消失的相關(guān)操作技巧,需要的朋友可以參考下2018-01-01Android應(yīng)用程序的編譯流程及使用Ant編譯項目的攻略
這篇文章主要介紹了Android應(yīng)用程序的編譯流程及使用Ant編譯項目的攻略,Ant是集編譯測試部署于一體的Java自動化工具,要的朋友可以參考下2016-04-04