@@ -7,23 +7,23 @@ const utils = require("tns-core-modules/utils/utils");
77import * as application from "tns-core-modules/application" ;
88import { device } from "tns-core-modules/platform" ;
99
10- let locationService = require ( './background-service' ) ;
11-
1210let page : Page ;
1311let model = new MainViewModel ( ) ;
1412let watchIds = [ ] ;
15- let backgroundIds = [ ] ;
13+ const jobId = 308 ; // the id should be unique for each background job. We only use one, so we set the id to be the same each time.
1614declare var com : any ;
1715
18- application . on ( application . exitEvent , function ( args : any ) {
19- if ( application . android && backgroundIds . length > 0 ) {
16+ function _stopBackgroundJob ( ) {
17+ if ( application . android ) {
2018 let context = utils . ad . getApplicationContext ( ) ;
2119 const jobScheduler = context . getSystemService ( ( < any > android . content . Context ) . JOB_SCHEDULER_SERVICE ) ;
22- const service = backgroundIds . pop ( ) ;
23- jobScheduler . cancel ( service ) ;
24- console . log ( `Job Canceled: ${ service } ` ) ;
20+ if ( jobScheduler . getPendingJob ( jobId ) !== null ) {
21+ jobScheduler . cancel ( jobId ) ;
22+ console . log ( `Job Canceled: ${ jobId } ` ) ;
23+ }
2524 }
26- } ) ;
25+ }
26+ application . on ( application . exitEvent , _stopBackgroundJob ) ;
2727
2828export function pageLoaded ( args : EventData ) {
2929 page = < Page > args . object ;
@@ -33,33 +33,26 @@ export function pageLoaded(args: EventData) {
3333export function startBackgroundTap ( ) {
3434 if ( application . android ) {
3535 let context = utils . ad . getApplicationContext ( ) ;
36- let intent = new android . content . Intent ( context , com . nativescript . location . BackgroundService . class ) ;
3736 if ( device . sdkVersion >= "26" ) {
38- const component = new android . content . ComponentName ( context , com . nativescript . location . BackgroundService26 . class ) ;
39- const builder = new ( < any > android . app ) . job . JobInfo . Builder ( 1 , component ) ;
40- builder . setRequiredNetworkType ( ( < any > android . app ) . job . JobInfo . NETWORK_TYPE_ANY ) ;
41- builder . setPeriodic ( 15 * 60 * 1000 ) ;
4237 const jobScheduler = context . getSystemService ( ( < any > android . content . Context ) . JOB_SCHEDULER_SERVICE ) ;
43- const service = jobScheduler . schedule ( builder . build ( ) ) ;
44- backgroundIds . push ( service ) ;
38+ const component = new android . content . ComponentName ( context , com . nativescript . location . BackgroundService26 . class ) ;
39+ const builder = new ( < any > android . app ) . job . JobInfo . Builder ( jobId , component ) ;
40+ builder . setOverrideDeadline ( 0 ) ;
41+ return jobScheduler . schedule ( builder . build ( ) ) ;
4542 } else {
43+ let intent = new android . content . Intent ( context , com . nativescript . location . BackgroundService . class ) ;
4644 context . startService ( intent ) ;
4745 }
4846 }
4947}
5048
5149export function stopBackgroundTap ( ) {
5250 if ( application . android ) {
53- let context = utils . ad . getApplicationContext ( ) ;
54- let intent = new android . content . Intent ( context , com . nativescript . location . BackgroundService . class ) ;
5551 if ( device . sdkVersion >= "26" ) {
56- if ( backgroundIds . length > 0 ) {
57- const jobScheduler = context . getSystemService ( ( < any > android . content . Context ) . JOB_SCHEDULER_SERVICE ) ;
58- const service = backgroundIds . pop ( ) ;
59- jobScheduler . cancel ( service ) ;
60- console . log ( `Job Canceled: ${ service } ` ) ;
61- }
52+ _stopBackgroundJob ( ) ;
6253 } else {
54+ let context = utils . ad . getApplicationContext ( ) ;
55+ let intent = new android . content . Intent ( context , com . nativescript . location . BackgroundService . class ) ;
6356 context . stopService ( intent ) ;
6457 }
6558 }
@@ -79,18 +72,17 @@ export function enableLocationTap() {
7972}
8073
8174export function buttonGetLocationTap ( ) {
82- let location = geolocation . getCurrentLocation ( {
75+ geolocation . getCurrentLocation ( {
8376 desiredAccuracy : Accuracy . high ,
8477 maximumAge : 5000 ,
8578 timeout : 10000
86- } )
87- . then ( function ( loc ) {
88- if ( loc ) {
89- model . locations . push ( loc ) ;
90- }
91- } , function ( e ) {
92- console . log ( "Error: " + ( e . message || e ) ) ;
93- } ) ;
79+ } ) . then ( function ( loc ) {
80+ if ( loc ) {
81+ model . locations . push ( loc ) ;
82+ }
83+ } , function ( e ) {
84+ console . log ( "Error: " + ( e . message || e ) ) ;
85+ } ) ;
9486}
9587
9688export function buttonStartTap ( ) {
@@ -106,7 +98,7 @@ export function buttonStartTap() {
10698 } ,
10799 {
108100 desiredAccuracy : Accuracy . high ,
109- updateDistance : 0. 1,
101+ updateDistance : 1 ,
110102 updateTime : 3000 ,
111103 minimumUpdateTime : 100
112104 } ) ) ;
0 commit comments