NativeScript Android 后台服务

NativeScript Android Background Service

我正在开发 NativeScript android 应用程序。我是 NativeScript 的新手。我想要 运行 后台服务,它会定期检查服务并向用户发出通知,即使应用程序未在设备上打开。我按照 Nativescript 网站的说明进行操作 https://www.nativescript.org/blog/using-android-background-services-in-nativescript

但是当我调用 setupAlarm 函数时,我收到错误 无法实例化 class com.tns.notifications.NotificationIntentService;没有空构造函数

请帮帮我。我在下面给出了完整的异常详细信息

The application crashed because of an uncaught exception. You can look at "stackTrace" or "nativeException" for more detailed information about the exception.
An uncaught Exception occurred on "main" thread.
java.lang.RuntimeException: Unable to instantiate service com.tns.notifications.NotificationIntentService: java.lang.InstantiationException: can't instantiate class com.tns.notifications.NotificationIntentService; no empty constructor
    at android.app.ActivityThread.handleCreateService(ActivityThread.java:2543)
    at android.app.ActivityThread.access00(ActivityThread.java:135)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:136)
    at android.app.ActivityThread.main(ActivityThread.java:5001)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
    at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.InstantiationException: can't instantiate class com.tns.notifications.NotificationIntentService; no empty constructor
    at java.lang.Class.newInstanceImpl(Native Method)
    at java.lang.Class.newInstance(Class.java:1208)
    at android.app.ActivityThread.handleCreateService(ActivityThread.java:2540)
    ... 10 more

最新版本的 Nativescript 允许您使用 workers for multithreading applications。我相信这是你应该使用的。在您提供的教程中,他们还提到将来使用网络工作者。

What’s next

In the upcoming releases we will be rolling out support for the Web Workers feature in NativeScript, allowing you to execute code in a background thread, all through Angular/JavaScript.

希望对您有所帮助。

我从这里找到了可用的 nativescript android 后台服务项目 https://github.com/NativeScript/sample-android-background-services

要解决 no empty constructor 问题,您可以这样使用 nativescript-android-utils

安装:

npm install nativescript-android-utils --save

在你的代码中:

com.pip3r4o.android.app.IntentService.extend("com.mypackage.MyClass", {
    onHandleIntent: function(){}
});

现在,com.mypackage.MyClass 将有一个空的构造函数。