Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 主活动外部的颤振调用方法通道和事件通道_Android_Flutter_Dart_Native - Fatal编程技术网

Android 主活动外部的颤振调用方法通道和事件通道

Android 主活动外部的颤振调用方法通道和事件通道,android,flutter,dart,native,Android,Flutter,Dart,Native,我找了很多,但没能解决我的问题。我想通过MethodChannel或EventChannel将数据从本机Android发送到Flatter。我编写了一个类NotificationGrabber,它扩展了NotificationListenerService,以便在Android上捕获通知。覆盖onNotificationPosted方法后,我成功捕获了传入通知。现在我正试图使用MethodChannel或EventChannel将这些通知发送到flifter,但这些构造函数需要一个二进制Mess

我找了很多,但没能解决我的问题。我想通过MethodChannelEventChannel将数据从本机Android发送到Flatter。我编写了一个类NotificationGrabber,它扩展了NotificationListenerService,以便在Android上捕获通知。覆盖onNotificationPosted方法后,我成功捕获了传入通知。现在我正试图使用MethodChannelEventChannel将这些通知发送到flifter,但这些构造函数需要一个二进制Messenger。我从使用flatterview作为BinaryMessenger的各种教程中看到(如果我错了,请纠正我)。由于onNotificationsPosted方法不在MainActivity中,我如何从NotificationGrabber类中执行此操作?如何使用另一个正确的BinaryMessenger?谢谢大家!

public class NotificationGrabber extends NotificationListenerService {
    private static final String CHANNEL = "mynotifications/getnotifications";
    private String TAG = this .getClass().getSimpleName() ;

    @Override
    public void onNotificationPosted (StatusBarNotification sbn) {
        Log. d ( TAG , "onNotificationPosted" ) ;
        sendNotificationsToFlutter(sbn);

    }

    private void sendNotificationsToFlutter(StatusBarNotification sbn) {
        Gson tempGson = new Gson();

        String jsonNotifications = tempGson.toJson(sbn);

        MethodChannel channel = new MethodChannel( <What could I put here?>, CHANNEL);

        channel.invokeMethod("returnedNotification", jsonNotifications );

    }
    .
    .
    .
    .
}
公共类NotificationGrabber扩展了NotificationListenerService{
私有静态最终字符串通道=“mynotifications/getnotifications”;
私有字符串标记=this.getClass().getSimpleName();
@凌驾
公告发布时的公共作废(状态公告sbn){
Log.d(标签“onNotificationPosted”);
向颤振发送通知(sbn);
}
私有无效发送通知至颤振(状态通知sbn){
Gson tempGson=新的Gson();
字符串jsonNotifications=tempGson.toJson(sbn);
MethodChannel=新的MethodChannel(,channel);
invokeMethod(“returnedNotification”,jsonNotifications);
}
.
.
.
.
}