Skip to content

Start service from native side (Kotlin/Java) #24

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
TheLastGimbus opened this issue Nov 12, 2021 · 2 comments
Closed

Start service from native side (Kotlin/Java) #24

TheLastGimbus opened this issue Nov 12, 2021 · 2 comments

Comments

@TheLastGimbus
Copy link

HI!
I would like to use a foreground service to update my native Android widget (on home screen) - I have everything set up and figured out, except the part "how to start foreground service with some Dart running when received onUpdate in widget"

Is there some nice Intent that I can just fire to start the service?

@Dev-hwang
Copy link
Owner

Dev-hwang commented Nov 15, 2021

It seems difficult to implement that way using this plugin. You will probably have to implement it yourself and use it.

Here's the implementation I'm thinking of:

  1. Get the CallbackHandle object using the PluginUtilities.getCallbackHandle function on the dart side and the toRawHandle function to get the raw value of the handler.
var rawValue = PluginUtilities.getCallbackHandle(callback)?.toRawHandle();
  1. Use MethodChannel to send the raw value of the handler to the native side.
static const methodChannel = MethodChannel('your_package/method');

methodChannel.invokeMethod('setRawValue', rawValue);
  1. Use SharedPreferences or SQLite in native to store the raw value of the handler.

  2. Get the raw value of the handler saved above in the widget's onUpdate function.

  3. Execute the dart side code using the FlutterEngine.dartExecutor.executeDartCallback function on the native side.

private fun executeDartCallback(callbackHandle: Long?) {
		// If there is no callback handle, the code below will not be executed.
		if (callbackHandle == null) return

		flutterEngine = FlutterEngine(this)

		flutterLoader = FlutterInjector.instance().flutterLoader()
		flutterLoader!!.startInitialization(this)
		flutterLoader!!.ensureInitializationComplete(this, null)

	        val appBundlePath = flutterLoader!!.findAppBundlePath()
		val callbackInfo = FlutterCallbackInformation.lookupCallbackInformation(callbackHandle)
		val dartCallback = DartExecutor.DartCallback(assets, appBundlePath, callbackInfo)
		flutterEngine!!.dartExecutor.executeDartCallback(dartCallback)
}

With this implementation, it seems that only the code on the dart side can be executed without running the foreground service. If I have time, I'll help you implement it.

@TheLastGimbus
Copy link
Author

Yeah, what i ended up doing is exactly this 👌 Thanks for help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants