|
1 | 1 | import * as geolocation from "nativescript-geolocation"; |
2 | 2 | import { Accuracy } from "tns-core-modules/ui/enums"; |
3 | 3 | import * as application from "tns-core-modules/application"; |
| 4 | +import { device } from "tns-core-modules/platform"; |
4 | 5 | import * as Toast from "nativescript-toast"; |
5 | 6 |
|
| 7 | +let watchId; |
| 8 | +application.on(application.exitEvent, function (args: any) { |
| 9 | + if (watchId) { |
| 10 | + geolocation.clearWatch(watchId); |
| 11 | + } |
| 12 | +}); |
| 13 | + |
6 | 14 | if (application.android) { |
7 | | - (<any>android.app.Service).extend("com.nativescript.location.BackgroundService", { |
8 | | - onStartCommand: function (intent, flags, startId) { |
9 | | - this.super.onStartCommand(intent, flags, startId); |
10 | | - return android.app.Service.START_STICKY; |
11 | | - }, |
12 | | - onCreate: function () { |
13 | | - let that = this; |
14 | | - geolocation.enableLocationRequest().then(function () { |
15 | | - that.id = geolocation.watchLocation( |
16 | | - function (loc) { |
17 | | - if (loc) { |
18 | | - let toast = Toast.makeText('Background Location: ' + loc.latitude + ' ' + loc.longitude); |
19 | | - toast.show(); |
20 | | - console.log('Background Location: ' + loc.latitude + ' ' + loc.longitude); |
21 | | - } |
22 | | - }, |
23 | | - function (e) { |
24 | | - console.log("Background watchLocation error: " + (e.message || e)); |
25 | | - }, |
26 | | - { |
27 | | - desiredAccuracy: Accuracy.high, |
28 | | - updateDistance: 0.1, |
29 | | - updateTime: 3000, |
30 | | - minimumUpdateTime: 100 |
31 | | - }); |
32 | | - }, function (e) { |
33 | | - console.log("Background enableLocationRequest error: " + (e.message || e)); |
34 | | - }); |
35 | | - }, |
36 | | - onBind: function (intent) { |
37 | | - console.log("on Bind Services"); |
38 | | - }, |
39 | | - onUnbind: function (intent) { |
40 | | - console.log('UnBind Service'); |
41 | | - }, |
42 | | - onDestroy: function () { |
43 | | - console.log('service onDestroy'); |
44 | | - geolocation.clearWatch(this.id); |
45 | | - } |
46 | | - }); |
| 15 | + if (device.sdkVersion < "26") { |
| 16 | + (<any>android.app.Service).extend("com.nativescript.location.BackgroundService", { |
| 17 | + onStartCommand: function (intent, flags, startId) { |
| 18 | + this.super.onStartCommand(intent, flags, startId); |
| 19 | + return android.app.Service.START_STICKY; |
| 20 | + }, |
| 21 | + onCreate: function () { |
| 22 | + let that = this; |
| 23 | + geolocation.enableLocationRequest().then(function () { |
| 24 | + that.id = geolocation.watchLocation( |
| 25 | + function (loc) { |
| 26 | + if (loc) { |
| 27 | + let toast = Toast.makeText('Background Location: ' + loc.latitude + ' ' + loc.longitude); |
| 28 | + toast.show(); |
| 29 | + console.log('Background Location: ' + loc.latitude + ' ' + loc.longitude); |
| 30 | + } |
| 31 | + }, |
| 32 | + function (e) { |
| 33 | + console.log("Background watchLocation error: " + (e.message || e)); |
| 34 | + }, |
| 35 | + { |
| 36 | + desiredAccuracy: Accuracy.high, |
| 37 | + updateDistance: 0.1, |
| 38 | + updateTime: 3000, |
| 39 | + minimumUpdateTime: 100 |
| 40 | + }); |
| 41 | + }, function (e) { |
| 42 | + console.log("Background enableLocationRequest error: " + (e.message || e)); |
| 43 | + }); |
| 44 | + }, |
| 45 | + onBind: function (intent) { |
| 46 | + console.log("on Bind Services"); |
| 47 | + }, |
| 48 | + onUnbind: function (intent) { |
| 49 | + console.log('UnBind Service'); |
| 50 | + }, |
| 51 | + onDestroy: function () { |
| 52 | + console.log('service onDestroy'); |
| 53 | + geolocation.clearWatch(this.id); |
| 54 | + } |
| 55 | + }); |
| 56 | + } |
| 57 | + else { |
| 58 | + (<any>android.app).job.JobService.extend("com.nativescript.location.BackgroundService26", { |
| 59 | + onStartJob(params) { |
| 60 | + let executed = false; |
| 61 | + geolocation.enableLocationRequest().then(function () { |
| 62 | + watchId = geolocation.watchLocation( |
| 63 | + function (loc) { |
| 64 | + if (loc) { |
| 65 | + let toast = Toast.makeText('Background Location: ' + loc.latitude + ' ' + loc.longitude); |
| 66 | + toast.show(); |
| 67 | + console.log('Background Location: ' + loc.latitude + ' ' + loc.longitude); |
| 68 | + } |
| 69 | + executed = true; |
| 70 | + }, |
| 71 | + function (e) { |
| 72 | + console.log("Background watchLocation error: " + (e.message || e)); |
| 73 | + executed = true; |
| 74 | + }, |
| 75 | + { |
| 76 | + desiredAccuracy: Accuracy.high, |
| 77 | + updateDistance: 0.1, |
| 78 | + updateTime: 3000, |
| 79 | + minimumUpdateTime: 100 |
| 80 | + }); |
| 81 | + }, function (e) { |
| 82 | + console.log("Background enableLocationRequest error: " + (e.message || e)); |
| 83 | + }); |
| 84 | + |
| 85 | + return executed; |
| 86 | + }, |
| 87 | + |
| 88 | + onStopJob() { |
| 89 | + console.log('service onStopJob'); |
| 90 | + geolocation.clearWatch(watchId); |
| 91 | + return true; |
| 92 | + }, |
| 93 | + }); |
| 94 | + } |
47 | 95 | } |
0 commit comments