当前位置:网站首页>Broadcastreceiver (broadcast)
Broadcastreceiver (broadcast)
2022-07-27 19:52:00 【Ashurol】
What is radio , The role of broadcasting ,Android Why design radio ?
In short , Broadcast is a mechanism for information feedback in applications , Broadcasting needs the help of BroadcastReceiver( Broadcast receivers ) receive , When sent , All have been registered BroadcastReceiver Will check the registration IntentFilter Whether it is related to the sent Intent(intent3.setAction(xx)) Match , If it matches, it will call BroadcastReceiver Class onReceive() Method , And Activity The components are different , When the system passes Intent Startup specifies Activity When the component , If the system does not find the right Activity when , Cause program to terminate abnormally , But the system did not find the right BroadcastReceiver Components , There is nothing wrong with the application .
Broadcasting is divided into ordinary broadcasting , Orderly broadcasting and asynchronous broadcasting
Ordinary and orderly are the same and different : The broadcast receiving order of the same level is random , After receiving the low-level broadcast , And dynamic registration at the same level is higher than static registration . Ordinary broadcasting cannot transmit the processing result to the next receiver and cannot stop broadcasting , Orderly broadcasting can .
The use of radio , Send a broadcast , Receive a broadcast
// Create a class , The interface is simple, and the sending is common , Orderly , Asynchronous broadcast
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Dynamically register broadcast recipients BroadcastReceiver
// IntentFilter intentfilter = new IntentFilter("BC_One");
// BC2 bc2 = new BC2();
// registerReceiver(bc2, intentfilter);
}
public void doClick(View v){
switch (v.getId()) {
case R.id.send1:// Send a normal broadcast
Intent intent = new Intent();
intent.putExtra("msg", " This is an ordinary broadcast ");
// And registration BroadcastReceiver At the time of the IntentFilter Of <action android:name="BC_One" /> Corresponding
intent.setAction("BC_One");
sendBroadcast(intent);// Send general broadcast
break;
case R.id.send2:// Send a normal broadcast
Intent intent2 = new Intent();
intent2.putExtra("msg", " This is an orderly broadcast ");
intent2.setAction("BC_One");
sendOrderedBroadcast(intent2, null);// Send an orderly broadcast
break;
case R.id.send3:// Send a normal broadcast
Intent intent3 = new Intent();
intent3.putExtra("msg", " This is an asynchronous broadcast ");
intent3.setAction("BC_Three");
sendStickyBroadcast(intent3);// Send asynchronous broadcast
// Asynchronous broadcasting can be sent first and then registered , But remember to add permissions <uses-permission android:name="android.permission.BROADCAST_STICKY"/>
IntentFilter intentfilter = new IntentFilter("BC_Three");
BC3 bc3 = new BC3();
registerReceiver(bc3, intentfilter);
break;
}
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
// Uninstall broadcast
// unregisterReceiver(receiver);
}
}BC1,BC2,BC3 Three categories
public class BC1 extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
String s = intent.getStringExtra("msg");
System.out.println("reveiver1 Received a message :"+s);
//abortBroadcast();// Execute interrupt broadcast , Contrast the difference between ordinary and orderly
// Processing dissemination data
Bundle bundle = new Bundle();
bundle.putString("test", " Broadcast processed data ");
setResultExtras(bundle);
}
}
public class BC2 extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
String s = intent.getStringExtra("msg");
System.out.println("reveiver2 Received a message :"+s);
Bundle bundle = getResultExtras(true);
String s2 = bundle.getString("test");
System.out.println(" The result is :"+s2);
}
}public class BC3 extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
System.out.println(" Received asynchronous broadcast !");
}
}Static configuration BroadcastReceiver Methods
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.imooc.broadcastreceiverdemo.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="com.imooc.broadcastreceiverdemo.BC1" >
<intent-filter>
<action android:name="BC_One" />
</intent-filter>
</receiver>
<receiver android:name="com.imooc.broadcastreceiverdemo.BC2" >
<intent-filter>
<action android:name="BC_One" />
</intent-filter>
</receiver>
</application>
Click the normal broadcast button to run the results


Click the result of the send order button
// Whether the test is terminated

// Test whether the information can be processed

The result of clicking the asynchronous send button

边栏推荐
猜你喜欢

Detailed interpretation of IEC104 protocol (I) protocol structure

Detailed explanation of the underlying data structure of redis

二叉搜索树

应用程序池已被禁用

GestureDetector(手势识别)

Hyperledger caliper is built on fabric for performance test

JS 事件监听 鼠标 键盘 表单 页面 onclick onkeydown onChange

Map和Set

Use of jvisualvm

ContentProvider of four components
随机推荐
Matplotlib (basic usage)
SystemService(系统服务)
VS2017#include 'xxx.h'
Application pool has been disabled
英特尔推出全球最小的高分辨率激光雷达,售价仅349美元
[basic knowledge of deep learning - 49] kmeans
View pagoda PHP extension directory
Embedded C language loop deployment
[deep learning target detection series - 01] what is target detection
UnicodeDecodeError: ‘utf-8‘ codec can‘t decode byte 0xff in position 0: invalid start byte
Embedded C language structure
IEC104 规约详细解读(一) 协议结构
ContentProvider of four components
[basic knowledge of deep learning - 45] distance calculation methods commonly used in machine learning
坚持软硬一体化,一恒科发力智能化教育机器人市场
四大组件之ContentProvider
Release Samsung 3J1 sensor: the code implies that the safety of pixel 7 face recognition will be greatly increased
【华为云Stack】【大架光临】第13期:管理区解耦架构见过吗?帮政企客户搞定大难题
Complex number proof of solvability of regular 17 sided ruler and gauge drawing
Come to sword finger offer 03. Repeated numbers in the array