I have a NativeScript-Angular App that get data from an API. To collect the data there are diffrent (angular) services with diffrent (api) endpoints. One type of data are very important therefor the app have to look for new data in a short interval (pulling), even the app is in background. The right choice for this task seems to be the android background services.
After a time of searching for a beast practice or examples, i found the nativescript-geolocation plugin with its demo + background-service.ts and the issue #157 where an angular-example attached. In this two examples and a few others the main action is a console.log(...)
, nice for the first try but not for a real app.
I want to use the existing service to fetch and handle the data from the API. I tried the method from Bass How to use (angular) HTTP Client in native background service - NativeScript with not full success. The injector gives me instances from my services but new once and not the instances from the main task. This prevent me to access all local app data with was initialized by the app at start time.
Following my example to get the android background service running and output a counter. For testing i call the length()
method from my ExampleService which return in this case always 0 because the array is empty in this instance from ExampleService. In the main instance from ExampleService the array has entries.
AndroidMainifest.xml - define the android service
<service android:name="com.tns.ExampleBackgroundService"
android:exported="false">
</service>
ExampleBackgroundService.ts
import { Injectable, Injector } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { DatabaseService } from '~/services/database.service';
import { MessageService } from '~/services/message.service';
@JavaProxy('com.tns.ExampleBackgroundService')
// @ts-ignore TS2304
export class ExampleBackgroundService extends (<any>android).app.Service {
protected injector: Injector;
protected exampleService: ExampleService;
protected id: number;
constructor() {
super();
return global.__native(this);
}
onCreate() {
this.injector = Injector.create({
providers:
[
{provide: HttpClient, useClass: HttpClient, deps: []},
{provide: DatabaseService, useClass: DatabaseService, deps: []},
{provide: MessageService, useClass: MessageService, deps: []},
{
provide: ExampleService,
useClass: ExampleService,
deps: [HttpClient, DatabaseService, MessageService]
},
]
});
}
onStartCommand(intent, flags, startId) {
this.super.onStartCommand(intent, flags, startId);
this.exampleService = this.injector.get(ExampleService);
let count = 0;
// @ts-ignore T2322
this.id = setInterval(() => {
console.log('count: ', count++, this.exampleService.length());
}, 1000
);
// @ts-ignore TS2304
return (<any>android).app.Service.START_STICKY;
}
}
start background service
const utils = require('tns-core-modules/utils/utils');
// ...
let context = utils.ad.getApplicationContext();
// @ts-ignore
let intent = new (<any>android).content.Intent(context, ExampleBackgroundService.class);
context.startService(intent);
How can i access the local data from the main task and how can i interact with the angular services from the main task?