Basic use Anroid [study notes] broadcast receivers

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/A1344714150/article/details/100155549

concept

Standard Broadcasting

A completely asynchronous broadcast executed, almost all broadcast receivers can receive this broadcast at the same time, it can not be truncated.

 

Ordered Broadcast

Broadcasting a synchronous execution, at the same time only a broadcast receiver to receive broadcast messages this provision, a higher priority to the broadcast receiver receives a broadcast message, it may be truncated.

 

Broadcast receiving system

Registered broadcasting, there are two general ways, registration and registration code in AndroidManifest.xml, the former is called dynamic registration, which is called a static register.

 

Dynamic registration

Advantages: flexibility to control the broadcast receiver registration and deregistration.

The core code to achieve:

onCreater():{ 
intentFilter = new IntentFilter(); 
//监听网络状态切换时系统发出的广播消息对应的action 
intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
networkChangeReceiver = new NetworkChangeReceiver(); 
//注册广播接收器 
registerReceiver(networkChangeReceiver,intentFilter); 
} 
onDestroy(){
unregisterReceiver(networkChangeReceiver); 
} 
class NetworkChangeReceiver extends BroadcastReceiver{ 
onReceiver(){
 //填写接收到对应广播时执行的代码 ...... 
} 
}

 

Static registration

Advantages: able to receive the broadcast in the program is not started.

The core code to achieve:

<manifest ...> 
//权限配置 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> 
<application> 
//使用AS的快捷方式创建广播接收器时会自动生成receiver配置 
//Enable属性表示是否启用这个广播接收器 
//Exported属性表示是否允许这个广播接收器接收本程序以外的广播 
<receiver android:name=".BootCompleteReceiver" android:enable="true" android:exported="true"> 
//配置需要监听的广播对应的action 
<intent-filter> 
<action android:name="android.intent.action.BOOT_COMPLETED"/> 
</intent-filter> 
</receiver> 
</application> 
</manifest>

Precautions

1. onReceive () method can not add too much logic and time-consuming operation, because the broadcast receiver is not allowed to open the thread. When onReceive () method runs for a long time without an end, the program will error.

2. sensitive operations need to configure permission to proceed.

 

Send custom broadcast

Send broadcast standard

Core code:

onCreate(){ 
button.setOnClickListener(new View.OnClickListener(){ 
public void onClick(View v){ 
Intent intent = new Intent("com.example.broadcasttest.MY_BROADCAST"); 
//发送标准广播 
sendBroadcast(intent); 
}
});
}

 

Send orderly broadcast

Core code:

onCreate(){ 
button.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
Intent intent = new Intent("com.example.broadcasttest.MY_BROADCAST"); 
//发送有序广播,第二个参数是一个与权限相关的字符串 
sendOrderedBroadcast(intent,null); } 
});
}

 

Set the priority of the broadcast receiver:

<mainfest> 
<application> 
<receiver> 
//配置优先级为100 
<intent-filter android:priority="100"> 
<action android:name="com.example.broadcasttest.MY_BROADCAST"/> 
</intent-filter> 
</receiver> 
</application> 
</mainfest>

 

Truncated orderly broadcast:

onReceiver(){ 
//截断广播消息 
abortBroadcast(); 
}

 

Use local broadcast

Emitted using local broadcasting broadcast mechanism can be transmitted within the application, and can only broadcast receiver receives a broadcast from the present application issues.

 

The core code to achieve:

onCreate(){ 
//获取实例 
localBroadcastManager = LocalBrocastManager.getInstance(this); button.setOnClickListener(new View.OnClickListener(){ 
public void onClick(View v){ 
Intent intent = new Intent("com.example.broadcasttest.LOCAL_BROADCAST"); 
//发送本地广播 
localBroadcastManager.sendBroadcast(intent); } 
});
intentFilter = new IntentFilter(); intentFilter.addAction("com.example.broadcasttest.LOCAL_BROADCAST"); 
localReceiver = new LocalReceiver(); 
//注册本地广播监听器 
localBroadcastManager.regesterReceiver(localReceiver,intentFilter); 
} 
onDestroy(){ 
//注销本地广播监听器 
localBroadcastManager.unregisterReceiver(localReceiver); 
}

 

The advantage of using local broadcast:

1 do not have to worry about the issue of disclosure of data broadcasting.

2 other programs can not send a broadcast to our internal procedures.

3 is more efficient than sending a local broadcast global broadcast transmission system.

 

Guess you like

Origin blog.csdn.net/A1344714150/article/details/100155549