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

fix: Add implementation for background service for API 26 and above #162

Merged
merged 3 commits into from
Sep 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions demo/app/App_Resources/Android/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@
android:exported="false" >
</service>

<service android:name="com.nativescript.location.BackgroundService26"
android:permission="android.permission.BIND_JOB_SERVICE"
android:enabled="true"
android:exported="false">
</service>

<activity
android:name="com.tns.NativeScriptActivity"
android:label="@string/title_activity_kimera"
Expand Down
128 changes: 88 additions & 40 deletions demo/app/background-service.ts
Original file line number Diff line number Diff line change
@@ -1,47 +1,95 @@
import * as geolocation from "nativescript-geolocation";
import { Accuracy } from "tns-core-modules/ui/enums";
import * as application from "tns-core-modules/application";
import { device } from "tns-core-modules/platform";
import * as Toast from "nativescript-toast";

let watchId;
application.on(application.exitEvent, function (args: any) {
if (watchId) {
geolocation.clearWatch(watchId);
}
});

if (application.android) {
(<any>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") {
(<any>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 {
(<any>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;
},
});
}
}
36 changes: 34 additions & 2 deletions demo/app/main-page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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((<any>android.content.Context).JOB_SCHEDULER_SERVICE);
const service = backgroundIds.pop();
jobScheduler.cancel(service);
console.log(`Job Canceled: ${service}`);
}
});

export function pageLoaded(args: EventData) {
page = <Page>args.object;
page.bindingContext = model;
Expand All @@ -21,15 +34,34 @@ 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 (<any>android.app).job.JobInfo.Builder(1, component);
builder.setRequiredNetworkType((<any>android.app).job.JobInfo.NETWORK_TYPE_ANY);
builder.setPeriodic(15 * 60 * 1000);
const jobScheduler = context.getSystemService((<any>android.content.Context).JOB_SCHEDULER_SERVICE);
const service = jobScheduler.schedule(builder.build());
backgroundIds.push(service);
} else {
context.startService(intent);
}
}
}

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((<any>android.content.Context).JOB_SCHEDULER_SERVICE);
const service = backgroundIds.pop();
jobScheduler.cancel(service);
console.log(`Job Canceled: ${service}`);
}
} else {
context.stopService(intent);
}
}
}

Expand Down