senthil
senthil

Reputation: 51

NativeScript Android Background Service

I am developing NativeScript android application. I am new to NativeScript. I want to run background service, which is checking service periodically and give notifications to user even though app not opened on device. I followed instructions from Nativescript site https://www.nativescript.org/blog/using-android-background-services-in-nativescript

But when i call setupAlarm function, i received error can't instantiate class com.tns.notifications.NotificationIntentService; no empty constructor

please help me. i give full exception details below

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

Upvotes: 2

Views: 4634

Answers (3)

Nicu Surdu
Nicu Surdu

Reputation: 8301

To solve the no empty constructor issue, you can use nativescript-android-utils like this:

Install:

npm install nativescript-android-utils --save

In you code:

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

Now, com.mypackage.MyClass will have an empty constructor.

Upvotes: 2

senthil
senthil

Reputation: 51

i found working nativescript android background service project from here https://github.com/NativeScript/sample-android-background-services

Upvotes: 1

Luan Rios Campos
Luan Rios Campos

Reputation: 193

The latest release of Nativescript allows you to use workers for multithreading applications. I believe it's what you should be using. In the tutorial you provided they also mention using web workers in the future.

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.

Hope it helps.

Upvotes: 0

Related Questions