1

So, here i'm trying to implement my MethodChannel on Flutter.

Here are some of my code snippets.

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.mypackage">

<!-- rest of the code -->

<application
    android:requestLegacyExternalStorage="true"
    android:allowBackup="false"
    android:name=".Application"
    android:label="My App Name"
    android:usesCleartextTraffic="true"
    tools:replace="android:label"
    android:icon="@mipmap/ic_launcher">

    <service
        android:name=".MyService"
        android:enabled="true"
        android:exported="true" />

    <activity
        android:name=".MainActivity"
        android:launchMode="singleTop"
        android:theme="@style/LaunchTheme"
        android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
        android:hardwareAccelerated="true"
        android:windowSoftInputMode="adjustResize">
        <intent-filter>
            <action android:name="FLUTTER_NOTIFICATION_CLICK" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>

        <meta-data
          android:name="io.flutter.embedding.android.NormalTheme"
          android:resource="@style/NormalTheme"
          />
        <meta-data
          android:name="io.flutter.embedding.android.SplashScreenDrawable"
          android:resource="@drawable/launch_background"
          />
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>

    <meta-data
        android:name="flutterEmbedding"
        android:value="2" />

    <!-- rest of the code -->
    
</application>

And this is how my MainActivity looks like

public class MainActivity extends FlutterActivity {

    private Intent forService;

@Override
public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {

    GeneratedPluginRegistrant.registerWith(flutterEngine);

    forService = new Intent(this, MyService.class);
    new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), "com.mypackage.messages")
            .setMethodCallHandler(
                    (methodCall, result) -> {
                        Log.d("method", "method " + methodCall.method);
                        if (methodCall.method.equals("startService")) {
                            startService();
                            result.success("Service Started");
                        }

                        if (methodCall.method.equals("stopService")) {
                            stopService();
                            result.success("Service Stoped");
                        }
                    }
            );

}

private void startService() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        startForegroundService(forService);
    } else {
        startService(forService);
    }
}

private void stopService() {
    stopService(forService);
}

}

And this is how i'm calling my own MethodChannel from the Dart's side.

  if (Platform.isAndroid) {
    var methodChannel = MethodChannel("com.mypackage.messages");
    String data = await methodChannel.invokeMethod("startService");
    debugPrint(data);
  }

I put the above code in main.dart. Actually, i'm using an AlarmManager whereby the callback would be called at specific time. And that callback should invoke the method 'startService' i've already defined on my MethodChannel.

Yet when the time comes up and it invokes the method, it says

Unhandled Exception: MissingPluginException(No implementation found for method startService on channel com.mypackage.messages)

I found many answers in SO and Github discussing about similar error, except that all the cases mentioned there occurred when someone is implementing other plugins. In this case, i'm doing my own MethodChannel.

So i was just wondering what part of MethodChannel configuration i might be missing here, or there is something i'm doing wrong. Any helps would be very much appreciated.

M Rijalul Kahfi
  • 1,460
  • 3
  • 22
  • 42

0 Answers0