Search code examples
androidnativescriptangular2-nativescriptandroid-broadcastreceiverbackground-service

NativeScript background service stops working after app closes


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);

Solution

  • 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.