Unity 获取安卓通知栏信息,在通知栏新增,删除时回调。Unity 和 Java 的调用和回调还是和之前一样:交互
首先定义一个java类继承自NotificationListenerService
onNotificationPosted函数是在通知栏新增的时候回调。
onNotificationRemoved函数是在通知栏删除时回调。
public class PhoneNotifyServer extends NotificationListenerService {
private Handler mHandler;
static final String ACTION_LOCAL_BINDING = "local_binding";
public void setmHandler(Handler handler){
mHandler = handler;
}
@Override
public IBinder onBind(Intent intent) {
return ACTION_LOCAL_BINDING.equals(intent.getAction())
? new LocalBinder() : super.onBind(intent);
}
@Override
public void onNotificationPosted(StatusBarNotification sbn) {
// TODO Auto-generated method stub
AndroidBaseMessage.instance._callBack.DebugCallBack("增加");
Bundle extras = sbn.getNotification().extras;
NotifyData data = new NotifyData();
data.id = sbn.getId();
// 获取接收消息APP的包名
data.packetName = sbn.getPackageName();
// 获取接收消息的抬头
data.title = extras.getString(Notification.EXTRA_TITLE);
// 获取接收消息的内容
data.data = extras.getString(Notification.EXTRA_TEXT);
data.subText = extras.getString(Notification.EXTRA_SUB_TEXT);
Bitmap icon = extras.getParcelable(Notification.EXTRA_SMALL_ICON);
if(icon != null) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
icon.compress(Bitmap.CompressFormat.PNG, 100, baos);
data.icon = baos.toByteArray();
}
data.type = 1;
Message msg = Message.obtain(mHandler);
msg.obj = data;
mHandler.sendMessage(msg);
}
@Override
public void onNotificationRemoved(StatusBarNotification sbn) {
AndroidBaseMessage.instance._callBack.DebugCallBack("减少");
// TODO Auto-generated method stub
Bundle extras = sbn.getNotification().extras;
NotifyData data = new NotifyData();
// 获取接收消息APP的包名
data.packetName = sbn.getPackageName();
data.type = 2;
Message msg = Message.obtain(mHandler);
msg.obj = data;
mHandler.sendMessage(msg);
}
public class NotifyData
{
public int id;
public int type ;
public String packetName;
public String title;
public String data;
public String subText;
public byte[] icon;
}
public class LocalBinder extends Binder
{
public PhoneNotifyServer getService() {
return PhoneNotifyServer.this;
}
}
开启监听在Init函数中实现,定义另外一个类
@Override
public void Init(Context context, CallUnity callback) {
super.Init(context, callback);
this.context = context;
_callBack = callback;
String string = Settings.Secure.getString(context.getContentResolver(),
"enabled_notification_listeners");
if (!string.contains(PhoneNotifyServer.class.getName())) {
context.startActivity(new Intent(
"android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS"));
}
Intent intent = new Intent(PhoneNotifyServer.ACTION_LOCAL_BINDING);
intent.setComponent(new ComponentName("com.lightin.unityARMainView", "com.lightin.phonemessage.PhoneNotifyServer"));
context.bindService(intent, mServiceConnection, Context.BIND_AUTO_CREATE);
}
private ServiceConnection mServiceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
mNotificationListener = ((PhoneNotifyServer.LocalBinder)service).getService();
mNotificationListener.setmHandler(mHandler);
}
@Override
public void onServiceDisconnected(ComponentName name) {
mNotificationListener = null;
}
};
Handler mHandler = new Handler(){
@Override
public void handleMessage(@NonNull Message msg) {
PhoneNotifyServer.NotifyData data = (PhoneNotifyServer.NotifyData)msg.obj;
_callBack.DebugCallBack("增加");
_callBack.JavaObjectCallBack(data);
}
};
首先会判断权限和申请,然后再绑定监听。
Manifest要添加:
<service android:name="com.lightin.phonemessage.PhoneNotifyServer" //这是你的类名 ,下面都是固定的 android:enabled="true" android:exported="true" android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE"> <intent-filter> <action android:name="android.service.notification.NotificationListenerService" /> </intent-filter> </service>