簡體   English   中英

Flutter 使用 methodChannel 從 android 本機代碼發送消息到 dart

[英]Flutter Send message using methodChannel from android native code to dart

我正在嘗試使用方法通道從 MainActivity 發送消息到 dart 代碼,當應用程序打開或在最近的應用程序中時一切正常,但是當我卸載應用程序時,dart 代碼無法從方法通道接收任何內容,請任何人都可以幫助我,即使應用程序不工作,我應該怎么做才能讓 dart 代碼接收消息。 這是我的代碼。

MainActivity.java

public class MainActivity extends FlutterActivity{
    void callFlutter(){
            //binaryMessenger I construct is in onCreate
            methodChannel=new MethodChannel(binaryMessenger,Channel);
            methodChannel.invokeMethod("didRecieveTranscript","Helllo");
    }
}

main.dart

main(){
  WidgetsFlutterBinding.ensureInitialized();
  const MethodChannel channel = MethodChannel('com.todomessages/send');
  channel.setMethodCallHandler(__didRecieveTranscript);
}

Future<void> __didRecieveTranscript(MethodCall call){
  ToDoDB _dbHelper=new ToDoDB();
  switch(call.method){
    case "didRecieveTranscript":
      FlutterLogs.logInfo("Message", "Returned Call from 12 service", "");
      _dbHelper.getToDoList(TO_DO_Today_For_Home_Page).then((lst) {
          if (lst.length > 0) {
            FlutterLogs.logInfo("Message", "Returned Call from 12 service", "There is data");
            startService();
          }
          else
            FlutterLogs.logInfo("Message", "Returned Call from 12 service", "There is no data");

        });
      break;
      }
}
void startService() async {
  if (Platform.isAndroid) {
    var methodChannel = MethodChannel("com.to_do_list.messages");
    await methodChannel.invokeMethod("startService");
  }
}

后台進程(如您所提到的,在分離應用程序時仍應工作的代碼)不會開箱即用,您需要一些特定的配置/設置才能做到這一點。 這是一篇解決這個問題的好文章: https://medium.com/flutter/executing-dart-in-the-background-with-flutter-plugins-and-geofencing-2b3e40a1a124

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM