Фоновая служба конденсатора, Невозможно вызвать метод - PullRequest
0 голосов
/ 19 февраля 2020

Я пытаюсь сделать сервис android через пользовательский плагин конденсатора. но я получил ошибку.

это пользовательский плагин конденсатора внутри android / src / main / java:

его можно вызвать из ioni c app.i также установить @PluginMethod return типа нет. пакет io.ioni c .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: это 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();
    }  
}

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

вызов метода из ioni c приложения, например:

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

1 Ответ

0 голосов
/ 20 февраля 2020

, поскольку компонент Android (служба, получатель, действие) может инициировать выполнение службы с помощью метода startService (намерение). я должен назвать это из контекста;

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

}
...