4

I have problem with nativescript background service. I want to write application with background service. The service must be always run in background, even when the application will be stoped and closed.

I have used nativescript-android-utils for android intent services, but the service stops working after application closes. I have also tried to register broadcast receiver according to this but the result is same: when app is closed, the service also stops working.

I want to write gps tracker background service and that's why I need to be the service always running in background.

Service code:

declare var com: any;
@JavaProxy("org.nativescript.PresleyApp.Test")
export class Test extends com.pip3r4o.android.app.IntentService {
    protected onHandleIntent(intent: android.content.Intent): void {
        geolocation.enableLocationRequest().then(function() {
            this.watchId = geolocation.watchLocation(
                (loc) => {
                    if (loc) {
                        const toast = Toast.makeText("Background Location: " + loc.latitude + " " + loc.longitude);
                        toast.show();
                        console.log("Background Location: " + loc.latitude + " " + loc.longitude);
                    }
                },
                (e) => {
                    console.log("Background watchLocation error: " + (e.message || e));
                },
                {
                    desiredAccuracy: Accuracy.high,
                    updateDistance: 0.1,
                    updateTime: 3000,
                    minimumUpdateTime: 100
                });
        }, (e) => {
            console.log("Background enableLocationRequest error: " + (e.message || e));
        });
    }
}

Starting service with

const context = application.android.context;
const intent = new android.content.Intent();
intent.setClassName(context, "org.nativescript.PresleyApp.Test");
context.startService(intent);
  • 1
    If the service is stopped, it will be restarted automatically. Are you facing this issue in any specific version of Android? Did you follow the sample [here](https://github.com/NativeScript/sample-android-background-services)? – Manoj Nov 14 '18 at 14:48
  • I will try that later. Android version is 6.0.0. API 23. emulator: Genymotion. I have edited and pasted code in question – Shota Noniashvili Nov 14 '18 at 14:57
  • The sample not working on android 6.0.0 API 23, and even on 7.1.0 API 25 – Shota Noniashvili Nov 15 '18 at 06:16
  • You must be able to see in the ReadMe that the sample uses apis those are available only in API level 26 and above (Android 8) so would not work on lower versions. But still the procedure is same. – Manoj Nov 15 '18 at 09:13
  • I have figured out that later, I have changed the functionality of service (use simple http request instead of notification), but it works at first time only when the app is actually open, but after that stops working even if the app is in background – Shota Noniashvili Nov 15 '18 at 10:10
  • 1
    Do you have repo? Because it works on my end. I periodically connect to my server and update data, I extend from `android.app.Service` – Manoj Nov 15 '18 at 10:38
  • I will try to extend to android.app.Service, I have extended to "android.app.job.JobService" and trying to send request to server in every 15 minute – Shota Noniashvili Nov 15 '18 at 10:52
  • Please can you give me some example code how to extend to `android.app.Service`. I am very new at mobile development – Shota Noniashvili Nov 15 '18 at 11:16
  • Hi! I am using nativescript-geolocation for gps tracking and it stops tracking when app is minimized. Have you managed to make it work when it is minimized? – Silko May 15 '19 at 09:45

1 Answers1

4

So here is how I did my service,

    import * as timer from "timer";

    @JavaProxy("com.nativescript.DataSyncService")
    class DataSyncService extends android.app.Service {

        private timerId: number;

        onBind(): android.os.IBinder {
            return null;
        }

        onCreate(): void {
            super.onCreate();

            if (!this.timerId) {
                this.timerId = timer.setInterval(()=> {
                   // Run this every 4 hrs
                }, (1000 * 60 * 60 * 4));
            }
        }

        onStartCommand(intent: android.content.Intent, flags: number, startId: number): number {
            return android.app.Service.START_STICKY;
        }

        onDestroy(): void {
            super.onDestroy();
            timer.clearInterval(this.timerId);
        }
    }

Returning START_STICKY from onStartCommand makes the system to restart the service when the process (app) is killed.

Manoj
  • 21,753
  • 3
  • 20
  • 41