Android Broadcast
|字数总计:2.1k|阅读时长:11分钟|阅读量:|
动态注册Receiver接收系统广播
需求描述
当ToggleButton
的状态为不检测时,TextView
显示Hello Broad
字符串
当点击ToggleButton后,TextView显示当前电量信息(要求能够实时刷新)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" android:orientation="vertical" android:gravity="center_horizontal">
<ToggleButton android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/tb_broad" android:paddingVertical="20dp" android:textSize="30sp" android:textOn="检测" android:textOff="不检测" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toTopOf="@id/tv_broad"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/tv_broad" android:text="HelloWorld!" android:textSize="30sp"/>
</LinearLayout>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| package com.example.broadcasttest;
import androidx.appcompat.app.AppCompatActivity;
import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.BatteryManager; import android.os.Bundle; import android.util.Log; import android.widget.CompoundButton; import android.widget.TextView; import android.widget.ToggleButton;
public class MainActivity extends AppCompatActivity { private final String TAG = MainActivity.class.getSimpleName(); private ToggleButton mTbBroad; private TextView mTvBroad; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mTbBroad = findViewById(R.id.tb_broad); mTvBroad = findViewById(R.id.tv_broad); MyReceiver myReceiver = new MyReceiver(); mTbBroad.setOnCheckedChangeListener((CompoundButton cb, boolean b)->{ Log.d(TAG, Boolean.valueOf(b).toString()); if(b) { IntentFilter filter = new IntentFilter(); filter.addAction(Intent.ACTION_BATTERY_CHANGED); registerReceiver(myReceiver, filter); }else{ mTvBroad.setText("Hello Broad"); unregisterReceiver(myReceiver); } }); }
public class MyReceiver extends BroadcastReceiver { private final String TAG = MyReceiver.class.getSimpleName(); @Override public void onReceive(Context context, Intent intent) { Log.d(TAG, intent.getAction()); if(intent.getAction().equals(Intent.ACTION_BATTERY_CHANGED)) { Bundle extras = intent.getExtras(); int level = extras.getInt(BatteryManager.EXTRA_LEVEL); int scale = extras.getInt(BatteryManager.EXTRA_SCALE); int percent = (int)(((double)level/(double)scale)*100); mTvBroad.setText("电量为:"+percent+"%"); } } } }
|
静态注册Receiver
创建了一个静态的StaticReceiver后,代码如下
使用ide的新建静态Receiver之后,AndroidManifest.xml中就会自动插入以下代码
1 2 3 4
| <receiver android:name=".StaticReceiver" android:enabled="true" android:exported="true"></receiver>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| package com.example.broadcasttest;
import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.util.Log; import android.widget.Toast;
public class StaticReceiver extends BroadcastReceiver {
@Override public void onReceive(Context context, Intent intent) { Log.d("StaticReceiver", "onReceive"); Toast.makeText(context, "From static receive", Toast.LENGTH_SHORT).show(); } }
|
MainActivity.java中加入以下声明周期定义,
1 2 3 4 5 6 7 8 9
| @Override protected void onResume() { super.onResume(); mBtnStatic.setOnClickListener((View view)->{ Intent intent = new Intent(); intent.setClass(this, StaticReceiver.class); sendBroadcast(intent); }); }
|
普通广播定义
当点击ToggleButton时,若状态为普通广播,则通过广播修改TextView为Normal Broad
,否则改为默认的Hello Broad
onResume()生命周期中添加
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| NormalReceiver normalReceiver = new NormalReceiver(); mTbNormal.setOnCheckedChangeListener((CompoundButton cb, boolean b)->{ if(b) { IntentFilter filter = new IntentFilter(); filter.addAction("intent.normal"); registerReceiver(normalReceiver, filter);
Intent intent = new Intent(); intent.setAction("intent.normal"); sendBroadcast(intent); }else{ mTvBroad.setText("Hello Broad"); unregisterReceiver(normalReceiver); } });
|
定义NormalReceiver内部类
1 2 3 4 5 6 7 8
| public class NormalReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if(intent.getAction().equals("intent.normal")) { mTvBroad.setText("Normal Broad"); } } }
|
本地广播
效果同普通广播,只不过写代码的方式不同,仅能实现应用程序自身通信
onResume()生命周期中添加
1 2 3 4 5 6 7 8 9 10 11 12 13
| LocalReceiver localReceiver = new LocalReceiver(); LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(this); mTbLocal.setOnCheckedChangeListener((CompoundButton cb, boolean b)->{ if(b) { IntentFilter filter = new IntentFilter("intent.local"); localBroadcastManager.registerReceiver(localReceiver,filter); Intent intent = new Intent("intent.local"); localBroadcastManager.sendBroadcast(intent); }else{ mTvBroad.setText("Hello Broad"); localBroadcastManager.unregisterReceiver(localReceiver); } });
|
定义 LocalReceiver内部类
1 2 3 4 5 6 7 8
| public class LocalReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if(intent.getAction().equals("intent.local")) { mTvBroad.setText("Local Broad"); } } }
|
有序广播
onResume添加
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| Order1Receiver order1Receiver = new Order1Receiver(); Order2Receiver order2Receiver = new Order2Receiver(); mTbOrder.setOnCheckedChangeListener((CompoundButton cb, boolean b)->{ if(b) { IntentFilter filter1 = new IntentFilter("intent.order"); filter1.setPriority(99); registerReceiver(order1Receiver, filter1);
IntentFilter filter2 = new IntentFilter("intent.order"); filter2.setPriority(101); registerReceiver(order2Receiver, filter2);
Intent intent = new Intent("intent.order"); sendOrderedBroadcast(intent,null); }else{ mTvBroad.setText("Hello Broad"); unregisterReceiver(order1Receiver); unregisterReceiver(order2Receiver); } });
|
定义Receiver内部类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| public class Order1Receiver extends BroadcastReceiver {
@Override public void onReceive(Context context, Intent intent) { if(intent.getAction().equals("intent.order")) { mTvBroad.setText("Order1 Broad"); } } } public class Order2Receiver extends BroadcastReceiver {
@Override public void onReceive(Context context, Intent intent) { if(intent.getAction().equals("intent.order")) { mTvBroad.setText("Order2 Broad"); } } }
|
结果如下,
先执行优先级大的,所以先执行order2,最后执行order1
终止传播
修改Order2Receiver定义,加入终止传播的代码
1 2 3 4 5 6 7 8 9 10 11 12
| public class Order2Receiver extends BroadcastReceiver {
@Override public void onReceive(Context context, Intent intent) { if(intent.getAction().equals("intent.order")) { mTvBroad.setText("Order2 Broad"); setResultData("From Order2"); abortBroadcast(); } } }
|
完整代码如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
| package com.example.broadcasttest;
import androidx.appcompat.app.AppCompatActivity; import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.BatteryManager; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.CompoundButton; import android.widget.TextView; import android.widget.ToggleButton;
public class MainActivity extends AppCompatActivity { private final String TAG = MainActivity.class.getSimpleName(); private ToggleButton mTbBroad, mTbNormal,mTbLocal, mTbOrder; private TextView mTvBroad; private Button mBtnStatic; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mTbBroad = findViewById(R.id.tb_broad); mTvBroad = findViewById(R.id.tv_broad); mBtnStatic = findViewById(R.id.btn_static); mTbNormal = findViewById(R.id.tb_normal_broad); mTbLocal = findViewById(R.id.tb_local_broad); mTbOrder = findViewById(R.id.tb_order_broad);
MyReceiver myReceiver = new MyReceiver(); mTbBroad.setOnCheckedChangeListener((CompoundButton cb, boolean b)->{ Log.d(TAG, Boolean.valueOf(b).toString()); if(b) { IntentFilter filter = new IntentFilter(); filter.addAction(Intent.ACTION_BATTERY_CHANGED); registerReceiver(myReceiver, filter); }else{ mTvBroad.setText("Hello Broad"); unregisterReceiver(myReceiver); } }); }
@Override protected void onResume() { super.onResume(); mBtnStatic.setOnClickListener((View view)->{ Intent intent = new Intent(); intent.setClass(this, StaticReceiver.class); sendBroadcast(intent); });
NormalReceiver normalReceiver = new NormalReceiver(); mTbNormal.setOnCheckedChangeListener((CompoundButton cb, boolean b)->{ if(b) { IntentFilter filter = new IntentFilter(); filter.addAction("intent.normal"); registerReceiver(normalReceiver, filter);
Intent intent = new Intent(); intent.setAction("intent.normal"); sendBroadcast(intent); }else{ mTvBroad.setText("Hello Broad"); unregisterReceiver(normalReceiver); } });
LocalReceiver localReceiver = new LocalReceiver(); LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(this); mTbLocal.setOnCheckedChangeListener((CompoundButton cb, boolean b)->{ if(b) { IntentFilter filter = new IntentFilter("intent.local"); localBroadcastManager.registerReceiver(localReceiver,filter); Intent intent = new Intent("intent.local"); localBroadcastManager.sendBroadcast(intent); }else{ mTvBroad.setText("Hello Broad"); localBroadcastManager.unregisterReceiver(localReceiver); } });
Order1Receiver order1Receiver = new Order1Receiver(); Order2Receiver order2Receiver = new Order2Receiver(); mTbOrder.setOnCheckedChangeListener((CompoundButton cb, boolean b)->{ if(b) { IntentFilter filter1 = new IntentFilter("intent.order"); filter1.setPriority(99); registerReceiver(order1Receiver, filter1);
IntentFilter filter2 = new IntentFilter("intent.order"); filter2.setPriority(101); registerReceiver(order2Receiver, filter2);
Intent intent = new Intent("intent.order"); sendOrderedBroadcast(intent,null); }else{ mTvBroad.setText("Hello Broad"); unregisterReceiver(order1Receiver); unregisterReceiver(order2Receiver); } }); }
public class Order1Receiver extends BroadcastReceiver {
@Override public void onReceive(Context context, Intent intent) { if(intent.getAction().equals("intent.order")) { mTvBroad.setText("Order1 Broad"); } } } public class Order2Receiver extends BroadcastReceiver {
@Override public void onReceive(Context context, Intent intent) { if(intent.getAction().equals("intent.order")) { mTvBroad.setText("Order2 Broad"); setResultData("From Order2"); abortBroadcast(); } } } public class LocalReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if(intent.getAction().equals("intent.local")) { mTvBroad.setText("Local Broad"); } } }
public class NormalReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if(intent.getAction().equals("intent.normal")) { mTvBroad.setText("Normal Broad"); } } }
public class MyReceiver extends BroadcastReceiver { private final String TAG = MyReceiver.class.getSimpleName(); @Override public void onReceive(Context context, Intent intent) { Log.d(TAG, intent.getAction()); if(intent.getAction().equals(Intent.ACTION_BATTERY_CHANGED)) { Bundle extras = intent.getExtras(); int level = extras.getInt(BatteryManager.EXTRA_LEVEL); int scale = extras.getInt(BatteryManager.EXTRA_SCALE); int percent = (int)(((double)level/(double)scale)*100); mTvBroad.setText("电量为:"+percent+"%"); } } } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" android:orientation="vertical" android:gravity="center_horizontal">
<ToggleButton android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/tb_broad" android:paddingVertical="20dp" android:textSize="30sp" android:textOn="检测" android:textOff="不检测"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/tv_broad" android:text="HelloWorld!" android:textSize="30sp"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/btn_static" android:textSize="30sp" android:text="静态注册"/> <ToggleButton android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/tb_normal_broad" android:paddingVertical="20dp" android:textSize="30sp" android:textOn="普通广播" android:textOff="不注册普通广播"/> <ToggleButton android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/tb_local_broad" android:paddingVertical="20dp" android:textSize="30sp" android:textOn="本地广播" android:textOff="不注册本地广播"/> <ToggleButton android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/tb_order_broad" android:paddingVertical="20dp" android:textSize="30sp" android:textOn="有序广播" android:textOff="不注册有序广播"/>
</LinearLayout>
|