NativeScript Android后台服务

NativeScript Android后台服务,nativescript,angular2-nativescript,Nativescript,Angular2 Nativescript,我正在开发NativeScript android应用程序。我不熟悉NativeScript。我想运行后台服务,它定期检查服务,并向用户发出通知,即使应用程序未在设备上打开。我按照Nativescript站点的指示进行操作 但是当我调用setupAlarm函数时,我收到错误无法实例化类com.tns.notifications.NotificationIntentService;没有空构造函数 请帮帮我。我在下面给出了完整的异常细节 The application crashed becaus

我正在开发NativeScript android应用程序。我不熟悉NativeScript。我想运行后台服务,它定期检查服务,并向用户发出通知,即使应用程序未在设备上打开。我按照Nativescript站点的指示进行操作

但是当我调用setupAlarm函数时,我收到错误无法实例化类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.access$1800(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允许您使用。我相信这是你应该用的。在您提供的教程中,他们还提到将来使用web workers

下一步是什么

在即将发布的版本中,我们将推出对Web的支持 NativeScript中的Workers功能,允许您在 后台线程,全部通过Angular/JavaScript


希望能有所帮助。

我在这里找到了正在工作的nativescript android后台服务项目

要解决
无空构造函数的问题,可以这样使用:

安装:

npm install nativescript-android-utils --save
在您的代码中:

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

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

这是一个已知的限制-您暂时不能在NativeScript android中声明0参数构造函数。谢谢您的回复。我将尝试使用网络工作者。用户关闭应用程序后,网络工作者是否继续工作?老实说,我不完全确定。我想工作人员应该在应用程序上除了UI线程之外创建一个新线程,当应用程序处于挂起状态(最小化)时,线程在后台应该可以正常工作,但是如果应用程序终止,那么工作人员将停止。