This repository was archived by the owner on Nov 8, 2023. It is now read-only.
File tree 2 files changed +20
-3
lines changed
2 files changed +20
-3
lines changed Original file line number Diff line number Diff line change @@ -4,6 +4,13 @@ import * as application from "tns-core-modules/application";
4
4
import { device } from "tns-core-modules/platform" ;
5
5
import * as Toast from "nativescript-toast" ;
6
6
7
+ let watchId ;
8
+ application . on ( application . exitEvent , function ( args : any ) {
9
+ if ( watchId ) {
10
+ geolocation . clearWatch ( watchId ) ;
11
+ }
12
+ } ) ;
13
+
7
14
if ( application . android ) {
8
15
if ( device . sdkVersion < "26" ) {
9
16
( < any > android . app . Service ) . extend ( "com.nativescript.location.BackgroundService" , {
@@ -50,10 +57,9 @@ if (application.android) {
50
57
else {
51
58
( < any > android . app ) . job . JobService . extend ( "com.nativescript.location.BackgroundService26" , {
52
59
onStartJob ( params ) {
53
- let that = this ;
54
60
let executed = false ;
55
61
geolocation . enableLocationRequest ( ) . then ( function ( ) {
56
- that . id = geolocation . watchLocation (
62
+ watchId = geolocation . watchLocation (
57
63
function ( loc ) {
58
64
if ( loc ) {
59
65
let toast = Toast . makeText ( 'Background Location: ' + loc . latitude + ' ' + loc . longitude ) ;
@@ -80,7 +86,8 @@ if (application.android) {
80
86
} ,
81
87
82
88
onStopJob ( ) {
83
- geolocation . clearWatch ( this . id ) ;
89
+ console . log ( 'service onStopJob' ) ;
90
+ geolocation . clearWatch ( watchId ) ;
84
91
return true ;
85
92
} ,
86
93
} ) ;
Original file line number Diff line number Diff line change @@ -15,6 +15,16 @@ let watchIds = [];
15
15
let backgroundIds = [ ] ;
16
16
declare var com : any ;
17
17
18
+ application . on ( application . exitEvent , function ( args : any ) {
19
+ if ( application . android && backgroundIds . length > 0 ) {
20
+ let context = utils . ad . getApplicationContext ( ) ;
21
+ 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 } ` ) ;
25
+ }
26
+ } ) ;
27
+
18
28
export function pageLoaded ( args : EventData ) {
19
29
page = < Page > args . object ;
20
30
page . bindingContext = model ;
You can’t perform that action at this time.
0 commit comments