Android如何发送跨进程广播,代码怎么写
时间: 2024-01-21 20:19:13 浏览: 106
Android中发送跨进程广播需要使用到Binder机制,下面是一份简单的代码实现:
1. 创建一个自定义的广播接收器类MyBroadcastReceiver,继承自BroadcastReceiver,用于处理接收到的广播消息。
```java
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String message = intent.getStringExtra("message");
Log.d("MyBroadcastReceiver", "Received message: " + message);
}
}
```
2. 在发送端进程中,创建一个远程服务MyService,它会在onCreate()方法中注册一个广播接收器MyBroadcastReceiver。
```java
public class MyService extends Service {
private MyBroadcastReceiver receiver;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
receiver = new MyBroadcastReceiver();
IntentFilter filter = new IntentFilter("com.example.broadcasttest.ACTION");
registerReceiver(receiver, filter);
}
@Override
public void onDestroy() {
super.onDestroy();
unregisterReceiver(receiver);
}
}
```
3. 在发送端进程中,启动MyService服务,并使用Intent发送广播消息。
```java
// 启动MyService服务
Intent intent = new Intent(this, MyService.class);
startService(intent);
// 发送广播消息
Intent broadcastIntent = new Intent("com.example.broadcasttest.ACTION");
broadcastIntent.putExtra("message", "Hello, world!");
sendBroadcast(broadcastIntent);
```
4. 在接收端进程中,创建一个远程服务MyService,它会在onCreate()方法中注册一个广播接收器MyBroadcastReceiver,并将这个广播接收器的Stub对象传递给发送端进程。
```java
public class MyService extends Service {
private MyBroadcastReceiver receiver;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return new MyBinder();
}
@Override
public void onCreate() {
super.onCreate();
receiver = new MyBroadcastReceiver();
IntentFilter filter = new IntentFilter("com.example.broadcasttest.ACTION");
registerReceiver(receiver, filter);
}
@Override
public void onDestroy() {
super.onDestroy();
unregisterReceiver(receiver);
}
private class MyBinder extends IMyBroadcastReceiver.Stub {
@Override
public void onReceive(Intent intent) throws RemoteException {
receiver.onReceive(MyService.this, intent);
}
}
}
```
5. 在接收端进程中,创建一个远程服务连接对象MyServiceConnection,用于连接到发送端进程中的MyService服务,并获取该服务返回的MyBinder对象。
```java
private IMyBroadcastReceiver myBinder;
private ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
myBinder = IMyBroadcastReceiver.Stub.asInterface(iBinder);
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
myBinder = null;
}
};
// 启动MyService服务
Intent intent = new Intent(this, MyService.class);
bindService(intent, connection, Context.BIND_AUTO_CREATE);
```
6. 在接收端进程中,使用MyBinder对象的onReceive()方法接收广播消息。
```java
if (myBinder != null) {
Intent intent = new Intent();
intent.putExtra("message", "Hello, world!");
myBinder.onReceive(intent);
}
```
以上就是一份简单的跨进程广播的代码实现,需要注意的是:
1. 在发送端进程和接收端进程中都需要创建一个远程服务,并分别注册一个广播接收器。
2. 在接收端进程中,通过MyBinder对象的Stub实现类将广播消息传递给广播接收器处理。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)