欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

BroadcastReceiver動態(tài)注冊案例詳解

 更新時間:2022年08月26日 09:07:03   作者:磚廠打工仔  
這篇文章主要為大家詳細介紹了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)文章

最新評論