简体   繁体   中英

How to call Dart from Android in background

I'm trying to call Dart code from Android when either the Activity is in de background or when there is no activity at all. For example, when the alarmmanager has triggered.

This is my dart code:

void main() async {
  WidgetsFlutterBinding.ensureInitialized();

  runApp(MyApp());

  const MethodChannel _kChannel =
      MethodChannel('mychannel');
  _kChannel.setMethodCallHandler((call) {
    if (call.method == 'method1') {
      return ...;
    }
    if (call.method == 'method2') {
      return ...;
    }
    return Future.value();
  });
}

From Java:

Application.channel.invokeMethod(method, arguments, new MethodChannel.Result() {...});

channel is a static variable that is initialized from my mainactivity.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    GeneratedPluginRegistrant.registerWith(this);

    Application.channel = new MethodChannel(getFlutterView(), CHANNEL_ID);
}

This works when the activity is in the foreground. But when it is in the background I get the following notification: FlutterView.send called on a detached view, channel=mychannel

The call does not get executed. How do I call Dart from Android in the background?

EDIT :

My usecase is a purely Android usecase. This is basically the flow:

  1. The alarmmanager fires and the broadcast event is received. Dit is done native without libraries. This mostly happens when the app is not active.
  2. In the broadcastreceiver, read some data from Firebase and set a notification.
  3. Schedule a new alarm based on data from Firebase.

Within flutter/dart I already got the business logic to fetch the data from Firebase. Obviously I don't want to write al of that again, but now in Java.

I don't want to use the alarmmanager plugin and the local notifciations plugin because the both run in different isolates causing trouble.

The answer to the question is bit tricky. First, what is it that you want to execute from background? Are you running a background service or scheduling any job at specific time or at defined intervals?

If you want to schedule a backgorund job or a periodic task, that you want to execute frequently, you can have a look at https://pub.dev/packages/workmanager plugin which lets you schedule background jobs and let you execute dart code from background when your activity is not alive.

If you want to build your own background service in native code: Android/iOS, and want to call dart code from it, you should read the following articles:

The Tricky Task of Keeping Flutter Running

The Tricky Task of Keeping Flutter Running (Vol. 2)

I must say, running dart code from native background service is not a straightforward thing, you'll surely need to scratch your head to achieve what you want.

Plus, keep in mind the background execution limits in both Android and iOS. That's another topic to dig in once you're good with this.

ViewModel has a bigger scope than Activity Lifecycle. You can call your flutter code from inside your ViewModel class method instead of Activity. https://developer.android.com/topic/libraries/architecture/viewmodel

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM