Unity 获取安卓通知栏信息

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>

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值