电容器后台服务,无法调用方法

Capacitor background service, Unable to invoke method

我正在尝试通过电容器自定义插件提供 android 服务。 但是我出错了。

这是 android/src/main/java 中的自定义电容器插件:

它可以从 ionic app.i 中调用,同时将 @PluginMethod return 类型设置为 none。 包裹 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 服务: 这是我的服务

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();
    }  
}

我从 MainActivity 调用了 MyService,startService 将被触发,但之后我得到了错误。 可能是因为回调函数。我不知道。

  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));
  }

像这样从离子应用调用方法:

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

错误是:

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.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.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.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) 

因为 Android 组件(服务,接收器,activity)可以通过 startService(intent) 方法触发服务的执行。我必须从上下文中调用它;

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

}