How to start android background service in typescript



I’m trying to create android background service by using typescript.

So far I have following service class:

export class MyService extends {

    public onStartCommand(intent, flags, startId) {
        console.log('MyService start')
        this.onStartCommand(intent, flags, startId);

    public onCreate() {
        var count = 0
        console.log('MyService create')
        const id = timer.setInterval(() => {
            console.log('timer ticks:' + count)
        }, 1000);


In android manifest I have:

<service android:name="com.somepackage.MyService" android:exported="false" >

Problem is that I don’t know how to refer service correctly when I want to start it. I’m currently trying to start service like this but compiler does not recognize class package.

export function startWorker() {
    console.log('starting worker...')
    let context =
    var intent = new android.content.Intent(context, com.somepackage.MyService.class)


This doesn’t answer your question directly, but have you seen nativescript’s wrapper for workers? Maybe it does what you’re trying to accomplish.


I tried worker first but in my test it stopped execution when app goes to background.


Just to update that I managed to get android background service to work.

Needs additionally this utils package:


declare var com: any

export class MyService extends {

    protected onHandleIntent(intent: android.content.Intent): void {
        console.log('executing service')


JavaProxy service name needs to be added to android manifest:

<service android:name="" android:exported="false"/>

Starting service:

function startBackgroundService() {
    let context =
    var intent = new android.content.Intent()
    intent.setClassName(context, "")