我正在开发 NativeScript android 应用程序。我是 NativeScript 的新手。我想运行后台服务,该服务定期检查服务并向用户发出通知,即使应用程序未在设备上打开。我按照 Nativescript 网站的说明进行操作 https://www.nativescript.org/blog/using-android-background-services-in-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-android-utils像这样:
安装:
npm install nativescript-android-utils --save
在您的代码中:
com.pip3r4o.android.app.IntentService.extend("com.mypackage.MyClass", {
onHandleIntent: function(){}
});
现在,com.mypackage.MyClass
将有一个空的构造函数。
关于NativeScript Android 后台服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41629584/