diff --git a/demo/app/App_Resources/Android/AndroidManifest.xml b/demo/app/App_Resources/Android/AndroidManifest.xml index fa154c6..45ddd81 100644 --- a/demo/app/App_Resources/Android/AndroidManifest.xml +++ b/demo/app/App_Resources/Android/AndroidManifest.xml @@ -29,6 +29,12 @@ android:exported="false" > + + + android.app.Service).extend("com.nativescript.location.BackgroundService", { - onStartCommand: function (intent, flags, startId) { - this.super.onStartCommand(intent, flags, startId); - return android.app.Service.START_STICKY; - }, - onCreate: function () { - let that = this; - geolocation.enableLocationRequest().then(function () { - that.id = geolocation.watchLocation( - function (loc) { - if (loc) { - let toast = Toast.makeText('Background Location: ' + loc.latitude + ' ' + loc.longitude); - toast.show(); - console.log('Background Location: ' + loc.latitude + ' ' + loc.longitude); - } - }, - function (e) { - console.log("Background watchLocation error: " + (e.message || e)); - }, - { - desiredAccuracy: Accuracy.high, - updateDistance: 0.1, - updateTime: 3000, - minimumUpdateTime: 100 - }); - }, function (e) { - console.log("Background enableLocationRequest error: " + (e.message || e)); - }); - }, - onBind: function (intent) { - console.log("on Bind Services"); - }, - onUnbind: function (intent) { - console.log('UnBind Service'); - }, - onDestroy: function () { - console.log('service onDestroy'); - geolocation.clearWatch(this.id); - } - }); + if (device.sdkVersion < "26") { + (android.app.Service).extend("com.nativescript.location.BackgroundService", { + onStartCommand: function (intent, flags, startId) { + this.super.onStartCommand(intent, flags, startId); + return android.app.Service.START_STICKY; + }, + onCreate: function () { + let that = this; + geolocation.enableLocationRequest().then(function () { + that.id = geolocation.watchLocation( + function (loc) { + if (loc) { + let toast = Toast.makeText('Background Location: ' + loc.latitude + ' ' + loc.longitude); + toast.show(); + console.log('Background Location: ' + loc.latitude + ' ' + loc.longitude); + } + }, + function (e) { + console.log("Background watchLocation error: " + (e.message || e)); + }, + { + desiredAccuracy: Accuracy.high, + updateDistance: 0.1, + updateTime: 3000, + minimumUpdateTime: 100 + }); + }, function (e) { + console.log("Background enableLocationRequest error: " + (e.message || e)); + }); + }, + onBind: function (intent) { + console.log("on Bind Services"); + }, + onUnbind: function (intent) { + console.log('UnBind Service'); + }, + onDestroy: function () { + console.log('service onDestroy'); + geolocation.clearWatch(this.id); + } + }); + } + else { + (android.app).job.JobService.extend("com.nativescript.location.BackgroundService26", { + onStartJob(params) { + let executed = false; + geolocation.enableLocationRequest().then(function () { + watchId = geolocation.watchLocation( + function (loc) { + if (loc) { + let toast = Toast.makeText('Background Location: ' + loc.latitude + ' ' + loc.longitude); + toast.show(); + console.log('Background Location: ' + loc.latitude + ' ' + loc.longitude); + } + executed = true; + }, + function (e) { + console.log("Background watchLocation error: " + (e.message || e)); + executed = true; + }, + { + desiredAccuracy: Accuracy.high, + updateDistance: 0.1, + updateTime: 3000, + minimumUpdateTime: 100 + }); + }, function (e) { + console.log("Background enableLocationRequest error: " + (e.message || e)); + }); + + return executed; + }, + + onStopJob() { + console.log('service onStopJob'); + geolocation.clearWatch(watchId); + return true; + }, + }); + } } diff --git a/demo/app/main-page.ts b/demo/app/main-page.ts index 686bfbc..aadb2e0 100644 --- a/demo/app/main-page.ts +++ b/demo/app/main-page.ts @@ -5,13 +5,26 @@ import { Page } from "ui/page"; import { MainViewModel } from "./main-view-model"; const utils = require("tns-core-modules/utils/utils"); import * as application from "tns-core-modules/application"; +import { device } from "tns-core-modules/platform"; + let locationService = require('./background-service'); let page: Page; let model = new MainViewModel(); let watchIds = []; +let backgroundIds = []; declare var com: any; +application.on(application.exitEvent, function (args: any) { + if (application.android && backgroundIds.length > 0) { + let context = utils.ad.getApplicationContext(); + const jobScheduler = context.getSystemService((android.content.Context).JOB_SCHEDULER_SERVICE); + const service = backgroundIds.pop(); + jobScheduler.cancel(service); + console.log(`Job Canceled: ${service}`); + } +}); + export function pageLoaded(args: EventData) { page = args.object; page.bindingContext = model; @@ -21,7 +34,17 @@ export function startBackgroundTap() { if (application.android) { let context = utils.ad.getApplicationContext(); let intent = new android.content.Intent(context, com.nativescript.location.BackgroundService.class); - context.startService(intent); + if (device.sdkVersion >= "26") { + const component = new android.content.ComponentName(context, com.nativescript.location.BackgroundService26.class); + const builder = new (android.app).job.JobInfo.Builder(1, component); + builder.setRequiredNetworkType((android.app).job.JobInfo.NETWORK_TYPE_ANY); + builder.setPeriodic(15 * 60 * 1000); + const jobScheduler = context.getSystemService((android.content.Context).JOB_SCHEDULER_SERVICE); + const service = jobScheduler.schedule(builder.build()); + backgroundIds.push(service); + } else { + context.startService(intent); + } } } @@ -29,7 +52,16 @@ export function stopBackgroundTap() { if (application.android) { let context = utils.ad.getApplicationContext(); let intent = new android.content.Intent(context, com.nativescript.location.BackgroundService.class); - context.stopService(intent); + if (device.sdkVersion >= "26") { + if (backgroundIds.length > 0) { + const jobScheduler = context.getSystemService((android.content.Context).JOB_SCHEDULER_SERVICE); + const service = backgroundIds.pop(); + jobScheduler.cancel(service); + console.log(`Job Canceled: ${service}`); + } + } else { + context.stopService(intent); + } } }