Search code examples
android-serviceintentserviceionic-nativebackground-servicecapacitor

Capacitor background service, Unable to invoke method


i'm trying to make an android service via capacitor custom plugin. but i got error.

this is custom capacitor plugin inside android/src/main/java :

it could be called from ionic app.i also set @PluginMethod return type to none. package io.ionic.starter;

import com.getcapacitor.JSObject;
import com.getcapacitor.NativePlugin;
import com.getcapacitor.Plugin;
import com.getcapacitor.PluginCall;
import com.getcapacitor.PluginMethod;

@NativePlugin()
public class CustomNativePlugin extends Plugin {

    @PluginMethod(returnType=PluginMethod.RETURN_NONE)
    public void customService() {
        MainActivity mainActivity = new MainActivity();
        mainActivity.startService();
        call.resolve();
    }
}

android service : this is MyService

package io.ionic.starter;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.widget.Toast;

public class MyService extends Service {
    public MyService() {
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // Let it continue running until it is stopped.
        Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();
    }  
}

and from MainActivity i called MyService, startService will be triger but after that i got error. maybe because of call back function. i dont know.

  public void startService() {
     startService(new Intent(getBaseContext(), MyService.class));
  }

  // Method to stop the service
  public void stopService(View view) {
    stopService(new Intent(getBaseContext(), MyService.class));
  }

call method from ionic app like this:

import { Plugins } from "@capacitor/core";
const { CustomNativePlugin } = Plugins;
runService(){
    CustomNativePlugin.customService();
}

and the error is :

2020-02-19 16:25:34.988 28206-28338/io.ionic.starter E/Capacitor: Unable to execute plugin method
com.getcapacitor.PluginInvocationException: Unable to invoke method customService on plugin io.ionic.starter.CustomNativePlugin
    at com.getcapacitor.PluginHandle.invoke(PluginHandle.java:101)
    at com.getcapacitor.Bridge$2.run(Bridge.java:537)
    at android.os.Handler.handleCallback(Handler.java:790)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:164)
    at android.os.HandlerThread.run(HandlerThread.java:65)
 Caused by: java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Method.invoke(Native Method)
    at com.getcapacitor.PluginHandle.invoke(PluginHandle.java:99)
    at com.getcapacitor.Bridge$2.run(Bridge.java:537) 
    at android.os.Handler.handleCallback(Handler.java:790) 
    at android.os.Handler.dispatchMessage(Handler.java:99) 
    at android.os.Looper.loop(Looper.java:164) 
    at android.os.HandlerThread.run(HandlerThread.java:65) 
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
    at android.content.ComponentName.<init>(ComponentName.java:130)
    at android.content.Intent.<init>(Intent.java:5780)
    at io.ionic.starter.MainActivity.startService(MainActivity.java:26)
    at io.ionic.starter.CustomNativePlugin.customService(CustomNativePlugin.java:37)
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.getcapacitor.PluginHandle.invoke(PluginHandle.java:99) 
    at com.getcapacitor.Bridge$2.run(Bridge.java:537) 
    at android.os.Handler.handleCallback(Handler.java:790) 
    at android.os.Handler.dispatchMessage(Handler.java:99) 
    at android.os.Looper.loop(Looper.java:164) 
    at android.os.HandlerThread.run(HandlerThread.java:65) 

Solution

  • because of An Android component (service, receiver, activity) can trigger the execution of a service via the startService(intent) method. i have to call it from context;

    @PluginMethod(returnType = PluginMethod.RETURN_NONE)
    public void customService(PluginCall call) {
        call.resolve();
        Intent service = new Intent(getContext(), MyIntentService.class);
        getContext().startService(service);
    
    }