NativeScript Android 后台服务

标签 nativescript angular2-nativescript

我正在开发 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/

相关文章:

javascript - 如何拒绝 NativeScript 上的 promise - Angular 2

Angular 2 Nativescript 网络/移动应用程序

android - 如何在 NativeScript 中更改应用程序名称

IOS 自动生成配置文件和证书

angular - Nativescript 后退按钮没有重定向到正确的页面

ios - 如何从NativeScript调用Objective-C NSExpression(format:…)?

android-webview - 在 NativeScript 应用程序中加载本地 HTML/JavaScript/CSS

ios - Nativescript - 为 Nativescript 插件设置目标 IOS 版本

Android 上的 NativeScript Angular Basic 应用程序大小 > 160 MB

ios - 如何在IOS平台Nativescript中使用GridLayout codrrect