如何从android接收器将数据传递给flatter

4nkexdtk  于 2021-07-08  发布在  Java
关注(0)|答案(0)|浏览(253)

我正在尝试将数据传递给flatter,它来自接收器,一切正常,但唯一一件事是我无法将数据发送给flatter,我必须调用notificationreceiver.java中的events.success()方法,但我不知道如何做到这一点。我将events param传递给notificationreceiver的构造函数,但是清单文件说:“这个类应该提供一个默认构造函数(一个没有参数的公共构造函数)”。然后我搜索了一些解决这个错误的方法,找到了registerreceiver()方法,但效果不太好。那么我如何传递这些数据呢?你知道吗?
notificationreceiver.java文件:

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class NotificationReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    final String action = intent.getAction();

    if (action.equals("Done")) {
        Toast.makeText(context, "Task completed", Toast.LENGTH_SHORT).show();
    } else if (action.equals("Snooze")) {
        Toast.makeText(context, "Task snoozed", Toast.LENGTH_SHORT).show();
    }

    // pass action string to flutter, by calling events.success() method somehow
  }
}

mainactivity.java:

@Override
public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
    super.configureFlutterEngine(flutterEngine);
    final String methodChannel = "com.example.colorfultasks/notifications";
    final String eventChannel = "com.example.colorfultasks/events";

    service = new Intent(MainActivity.this, NotificationService.class);

    new EventChannel(getFlutterEngine().getDartExecutor().getBinaryMessenger(), eventChannel)
            .setStreamHandler(new EventChannel.StreamHandler() {
                @Override
                public void onListen(Object arguments, EventChannel.EventSink events) {
                    // I don't know what to do here
                }

                @Override
                public void onCancel(Object arguments) {}
            });

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题