Skip to content
This repository was archived by the owner on Nov 8, 2023. It is now read-only.

Commit 44f1cd2

Browse files
authored
Merge pull request #87 from NativeScript/tachev/add-background-location-demo
Added a background service for tosting the current location each 3 seconds in the Demo app
2 parents e938e86 + c18b726 commit 44f1cd2

File tree

9 files changed

+70
-57
lines changed

9 files changed

+70
-57
lines changed

demo/app/App_Resources/Android/AndroidManifest.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
package="__PACKAGE__"
44
android:versionCode="1"
55
android:versionName="1.0">
6-
76
<supports-screens
87
android:smallScreens="true"
98
android:normalScreens="true"
@@ -25,6 +24,11 @@
2524
android:label="@string/app_name"
2625
android:theme="@style/AppTheme">
2726

27+
28+
<service android:name="com.nativescript.location.BackgroundService"
29+
android:exported="false" >
30+
</service>
31+
2832
<activity
2933
android:name="com.tns.NativeScriptActivity"
3034
android:label="@string/title_activity_kimera"

demo/app/App_Resources/Android/app.gradle

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,3 @@ android {
1414
additionalParameters "--no-version-vectors"
1515
}
1616
}
17-
18-
def settingsGradlePath = "$projectDir/../../app/App_Resources/Android/settings.gradle";
19-
def settingsGradleFile = new File(settingsGradlePath);
20-
if(settingsGradleFile.exists())
21-
{
22-
apply from: settingsGradleFile;
23-
}

demo/app/App_Resources/Android/settings.gradle

Lines changed: 0 additions & 44 deletions
This file was deleted.

demo/app/App_Resources/Android/settings.json

Lines changed: 0 additions & 1 deletion
This file was deleted.

demo/app/background-service.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import * as geolocation from "nativescript-geolocation";
2+
import { Accuracy } from "tns-core-modules/ui/enums";
3+
import * as application from "tns-core-modules/application";
4+
import * as Toast from "nativescript-toast";
5+
6+
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+
geolocation.enableLocationRequest().then(function () {
14+
geolocation.watchLocation(
15+
function (loc) {
16+
if (loc) {
17+
let toast = Toast.makeText('Background Location: ' + loc.latitude + ' ' + loc.longitude);
18+
toast.show();
19+
console.log('Background Location: ' + loc.latitude + ' ' + loc.longitude);
20+
}
21+
},
22+
function (e) {
23+
console.log("Background watchLocation error: " + (e.message || e));
24+
},
25+
{
26+
desiredAccuracy: Accuracy.high,
27+
updateDistance: 0.1,
28+
updateTime: 3000,
29+
minimumUpdateTime: 100
30+
});
31+
}, function (e) {
32+
console.log("Background enableLocationRequest error: " + (e.message || e));
33+
});
34+
},
35+
onBind: function (intent) {
36+
console.log("on Bind Services");
37+
},
38+
onUnbind: function(intent){
39+
console.log('UnBind Service');
40+
},
41+
onDestroy: function() {
42+
console.log('service onDestroy');
43+
}
44+
});
45+
}

demo/app/main-page.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,28 @@ import { Accuracy } from "ui/enums";
33
import { EventData } from "data/observable";
44
import { Page } from "ui/page";
55
import { MainViewModel } from "./main-view-model";
6+
const utils = require("tns-core-modules/utils/utils");
7+
import * as application from "tns-core-modules/application";
8+
let locationService = require('./background-service');
69

710
let page: Page;
811
let model = new MainViewModel();
912
let watchIds = [];
13+
declare var com: any;
1014

1115
export function pageLoaded(args: EventData) {
1216
page = <Page>args.object;
1317
page.bindingContext = model;
1418
}
1519

20+
export function startBackgroundTap() {
21+
if (application.android) {
22+
let context = utils.ad.getApplicationContext();
23+
let intent = new android.content.Intent(context, com.nativescript.location.BackgroundService.class);
24+
context.startService(intent);
25+
}
26+
}
27+
1628
export function enableLocationTap() {
1729
geolocation.isEnabled().then(function (isEnabled) {
1830
if (!isEnabled) {

demo/app/main-page.xml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
<Page xmlns="http://schemas.nativescript.org/tns.xsd" loaded="pageLoaded" class="page">
2-
<GridLayout rows="auto, *, auto">
2+
<GridLayout rows="auto, auto, *, auto">
33
<GridLayout row="0" columns="*, *, *, *" >
44
<Button text="Enable Location" col="0" textWrap="true" tap="enableLocationTap"/>
55
<Button text="Get Current Location" col="1" textWrap="true" tap="buttonGetLocationTap"/>
66
<Button text="Start Monitoring" col="2" textWrap="true" tap="buttonStartTap"/>
77
<Button text="Stop Monitoring" col="3" textWrap="true" tap="buttonStopTap"/>
88
</GridLayout>
9-
<ListView row="1" items="{{ locations }}">
9+
<GridLayout row="1" columns="*" >
10+
<Button text="Start Background thread monitoring" col="0" ios:visibility="collapsed" textWrap="true" tap="startBackgroundTap"/>
11+
</GridLayout>
12+
<ListView row="2" items="{{ locations }}">
1013
<ListView.itemTemplate>
1114
<Label text="{{ $value, latitude + ', ' + longitude + ', ' + altitude }}" />
1215
</ListView.itemTemplate>
1316
</ListView>
14-
<Button text="Clear" row="2" tap="buttonClearTap"/>
17+
<Button text="Clear" row="3" tap="buttonClearTap"/>
1518
</GridLayout>
1619
</Page>

demo/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"dependencies": {
1212
"nativescript-geolocation": "../src",
1313
"nativescript-theme-core": "^1.0.4",
14+
"nativescript-toast": "^1.4.6",
1415
"nativescript-unit-test-runner": "^0.3.4",
1516
"tns-core-modules": "^3.0.0"
1617
},

src/geolocation.android.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ function _isGooglePlayServicesAvailable(): boolean {
215215

216216
let isLocationServiceEnabled = true;
217217
let googleApiAvailability = com.google.android.gms.common.GoogleApiAvailability.getInstance();
218-
let resultCode = googleApiAvailability.isGooglePlayServicesAvailable(androidAppInstance.foregroundActivity);
218+
let resultCode = googleApiAvailability.isGooglePlayServicesAvailable(androidAppInstance.context);
219219
if (resultCode !== com.google.android.gms.common.ConnectionResult.SUCCESS) {
220220
isLocationServiceEnabled = false;
221221
}

0 commit comments

Comments
 (0)