如何实时获取通信呢?
比如我的一个下载任务,我需要实时获取的它下载进度?
官方提供了 RemoteCallbackList 这个类,专门用于接口列表的繁琐工作,特别是用于 service 于 client 之间的回调。
具体来说:
跟踪一组已注册的IInterface回调,注意他们的底层需要时 binder 通信的,比如 aidl 的服务
通过 Binder.DeathRecipien 绑定已注册的接口,当进程清除时,也能清除列表
通过锁定底层接口从而处理多线程,以及一种线程安全的方式来迭代列表的快照而不持有其锁。
下面来看具体的实现:
服务端APP
aidl接口服务:
// INotifyService.aidl
import com.xxxx.mynotificationservice.ISystemUINotify;
// Declare any non-default types here with import statements
//定义些接口方法提供给客户端调用
interface INotifyService {
//注册回调
void registerCallback(ISystemUINotify notify);
//取消注册回调
void unregisterCallback(ISystemUINotify notify);
}
获取信息传递回调给客户端
// ISystemUINotify.aidl
package com.xxxxx.mynotificationservice;
// Declare any non-default types here with import statements
//主要获取服务端相关信息回调给客户端
interface ISystemUINotify {
void notifyMsg(String content);
}
服务端Service :
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteCallbackList;
import android.os.RemoteException;
import androidx.annotation.Nullable;
public class NotificationService extends Service {
private RemoteCallbackList<ISystemUINotify> remoteCallbackList = new RemoteCallbackList<>();
private INotifyService.Stub mMyBinder = new INotifyService.Stub() {
@Override
public void registerCallback(ISystemUINotify notify) throws RemoteException { //注册接口回调
remoteCallbackList.register(notify);
notify.notifyMsg("消息");
}
@Override
public void unregisterCallback(ISystemUINotify notify) throws RemoteException {
//取消注册接口回调
remoteCallbackList.unregister(notify);
}
};
@Nullable
@Override
public IBinder onBind(Intent intent) {
return mMyBinder;
}
@Override
public void onDestroy() {
super.onDestroy();
remoteCallbackList.kill();
}
}
服务端 mainfest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.xxxx.mynotificationservice">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".NotificationService"
android:enabled="true"
android:exported="true" />
</application>
</manifest>
客户端APP:
AIDL 与服务端相同 ,这里省略。
客户端绑定Service
package com.xxx.app2;
import android.app.Application;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import com.xxxx.mynotificationservice.INotifyService;
import com.xxxx.mynotificationservice.ISystemUINotify;
public class NotificationServiceApplication extends Application {
public static final String NOTIFICATION_SERVICE_PACKAGE_NAME = "com.xxxx.mynotificationservice";
public static final String NOTIFICATION_SERVICE = "com.xxxx.mynotificationservice.NotificationService";
public static INotifyService iNotifyService;
@Override
public void onCreate() {
super.onCreate();
Intent intent = new Intent();
ComponentName componentName = new ComponentName(
NOTIFICATION_SERVICE_PACKAGE_NAME,
NOTIFICATION_SERVICE);
intent.setComponent(componentName);
bindService(intent,serviceConnection,BIND_AUTO_CREATE);
Log.d("notification ","Notification Service has started");
}
ISystemUINotify.Stub iSystemUINotify = new ISystemUINotify.Stub() {
@Override
public void notifyMsg(String content) throws RemoteException {
Log.d("content", "收到服务端的消息:" + content);
}
};
public ServiceConnection serviceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
iNotifyService = INotifyService.Stub.asInterface(iBinder);
try {
iNotifyService.registerCallback(iSystemUINotify);
} catch (RemoteException e) {
e.printStackTrace();
}
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
if (iNotifyService != null) {
try {
iNotifyService.unregisterCallback(iSystemUINotify);
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
};
@Override
public void onTerminate() {
super.onTerminate();
unbindService(serviceConnection);
}
}
Mainfest文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.xxx.app2">
<application
android:name=".NotificationServiceApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>