NativeScript background service stops working after app closes










1















I have problem with nativescript background service. I want to write application with background service. The service must be always run in background, even when the application will be stoped and closed.



I have used nativescript-android-utils for android intent services, but the service stops working after application closes. I have also tried to register broadcast receiver according to this but the result is same:
when app is closed, the service also stops working.



I want to write gps tracker background service and that's why I need to be the service always running in background.



Service code:



declare var com: any;
@JavaProxy("org.nativescript.PresleyApp.Test")
export class Test extends com.pip3r4o.android.app.IntentService
protected onHandleIntent(intent: android.content.Intent): void
geolocation.enableLocationRequest().then(function()
this.watchId = geolocation.watchLocation(
(loc) =>
if (loc)
const toast = Toast.makeText("Background Location: " + loc.latitude + " " + loc.longitude);
toast.show();
console.log("Background Location: " + loc.latitude + " " + loc.longitude);

,
(e) => e));
,

desiredAccuracy: Accuracy.high,
updateDistance: 0.1,
updateTime: 3000,
minimumUpdateTime: 100
);
, (e) => e));
);




Starting service with



const context = application.android.context;
const intent = new android.content.Intent();
intent.setClassName(context, "org.nativescript.PresleyApp.Test");
context.startService(intent);









share|improve this question



















  • 1





    If the service is stopped, it will be restarted automatically. Are you facing this issue in any specific version of Android? Did you follow the sample here?

    – Manoj
    Nov 14 '18 at 14:48











  • I will try that later. Android version is 6.0.0. API 23. emulator: Genymotion. I have edited and pasted code in question

    – Shota Noniashvili
    Nov 14 '18 at 14:57











  • The sample not working on android 6.0.0 API 23, and even on 7.1.0 API 25

    – Shota Noniashvili
    Nov 15 '18 at 6:16











  • You must be able to see in the ReadMe that the sample uses apis those are available only in API level 26 and above (Android 8) so would not work on lower versions. But still the procedure is same.

    – Manoj
    Nov 15 '18 at 9:13






  • 1





    Do you have repo? Because it works on my end. I periodically connect to my server and update data, I extend from android.app.Service

    – Manoj
    Nov 15 '18 at 10:38















1















I have problem with nativescript background service. I want to write application with background service. The service must be always run in background, even when the application will be stoped and closed.



I have used nativescript-android-utils for android intent services, but the service stops working after application closes. I have also tried to register broadcast receiver according to this but the result is same:
when app is closed, the service also stops working.



I want to write gps tracker background service and that's why I need to be the service always running in background.



Service code:



declare var com: any;
@JavaProxy("org.nativescript.PresleyApp.Test")
export class Test extends com.pip3r4o.android.app.IntentService
protected onHandleIntent(intent: android.content.Intent): void
geolocation.enableLocationRequest().then(function()
this.watchId = geolocation.watchLocation(
(loc) =>
if (loc)
const toast = Toast.makeText("Background Location: " + loc.latitude + " " + loc.longitude);
toast.show();
console.log("Background Location: " + loc.latitude + " " + loc.longitude);

,
(e) => e));
,

desiredAccuracy: Accuracy.high,
updateDistance: 0.1,
updateTime: 3000,
minimumUpdateTime: 100
);
, (e) => e));
);




Starting service with



const context = application.android.context;
const intent = new android.content.Intent();
intent.setClassName(context, "org.nativescript.PresleyApp.Test");
context.startService(intent);









share|improve this question



















  • 1





    If the service is stopped, it will be restarted automatically. Are you facing this issue in any specific version of Android? Did you follow the sample here?

    – Manoj
    Nov 14 '18 at 14:48











  • I will try that later. Android version is 6.0.0. API 23. emulator: Genymotion. I have edited and pasted code in question

    – Shota Noniashvili
    Nov 14 '18 at 14:57











  • The sample not working on android 6.0.0 API 23, and even on 7.1.0 API 25

    – Shota Noniashvili
    Nov 15 '18 at 6:16











  • You must be able to see in the ReadMe that the sample uses apis those are available only in API level 26 and above (Android 8) so would not work on lower versions. But still the procedure is same.

    – Manoj
    Nov 15 '18 at 9:13






  • 1





    Do you have repo? Because it works on my end. I periodically connect to my server and update data, I extend from android.app.Service

    – Manoj
    Nov 15 '18 at 10:38













1












1








1








I have problem with nativescript background service. I want to write application with background service. The service must be always run in background, even when the application will be stoped and closed.



I have used nativescript-android-utils for android intent services, but the service stops working after application closes. I have also tried to register broadcast receiver according to this but the result is same:
when app is closed, the service also stops working.



I want to write gps tracker background service and that's why I need to be the service always running in background.



Service code:



declare var com: any;
@JavaProxy("org.nativescript.PresleyApp.Test")
export class Test extends com.pip3r4o.android.app.IntentService
protected onHandleIntent(intent: android.content.Intent): void
geolocation.enableLocationRequest().then(function()
this.watchId = geolocation.watchLocation(
(loc) =>
if (loc)
const toast = Toast.makeText("Background Location: " + loc.latitude + " " + loc.longitude);
toast.show();
console.log("Background Location: " + loc.latitude + " " + loc.longitude);

,
(e) => e));
,

desiredAccuracy: Accuracy.high,
updateDistance: 0.1,
updateTime: 3000,
minimumUpdateTime: 100
);
, (e) => e));
);




Starting service with



const context = application.android.context;
const intent = new android.content.Intent();
intent.setClassName(context, "org.nativescript.PresleyApp.Test");
context.startService(intent);









share|improve this question
















I have problem with nativescript background service. I want to write application with background service. The service must be always run in background, even when the application will be stoped and closed.



I have used nativescript-android-utils for android intent services, but the service stops working after application closes. I have also tried to register broadcast receiver according to this but the result is same:
when app is closed, the service also stops working.



I want to write gps tracker background service and that's why I need to be the service always running in background.



Service code:



declare var com: any;
@JavaProxy("org.nativescript.PresleyApp.Test")
export class Test extends com.pip3r4o.android.app.IntentService
protected onHandleIntent(intent: android.content.Intent): void
geolocation.enableLocationRequest().then(function()
this.watchId = geolocation.watchLocation(
(loc) =>
if (loc)
const toast = Toast.makeText("Background Location: " + loc.latitude + " " + loc.longitude);
toast.show();
console.log("Background Location: " + loc.latitude + " " + loc.longitude);

,
(e) => e));
,

desiredAccuracy: Accuracy.high,
updateDistance: 0.1,
updateTime: 3000,
minimumUpdateTime: 100
);
, (e) => e));
);




Starting service with



const context = application.android.context;
const intent = new android.content.Intent();
intent.setClassName(context, "org.nativescript.PresleyApp.Test");
context.startService(intent);






android nativescript angular2-nativescript android-broadcastreceiver background-service






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 14:59







Shota Noniashvili

















asked Nov 14 '18 at 14:30









Shota NoniashviliShota Noniashvili

748




748







  • 1





    If the service is stopped, it will be restarted automatically. Are you facing this issue in any specific version of Android? Did you follow the sample here?

    – Manoj
    Nov 14 '18 at 14:48











  • I will try that later. Android version is 6.0.0. API 23. emulator: Genymotion. I have edited and pasted code in question

    – Shota Noniashvili
    Nov 14 '18 at 14:57











  • The sample not working on android 6.0.0 API 23, and even on 7.1.0 API 25

    – Shota Noniashvili
    Nov 15 '18 at 6:16











  • You must be able to see in the ReadMe that the sample uses apis those are available only in API level 26 and above (Android 8) so would not work on lower versions. But still the procedure is same.

    – Manoj
    Nov 15 '18 at 9:13






  • 1





    Do you have repo? Because it works on my end. I periodically connect to my server and update data, I extend from android.app.Service

    – Manoj
    Nov 15 '18 at 10:38












  • 1





    If the service is stopped, it will be restarted automatically. Are you facing this issue in any specific version of Android? Did you follow the sample here?

    – Manoj
    Nov 14 '18 at 14:48











  • I will try that later. Android version is 6.0.0. API 23. emulator: Genymotion. I have edited and pasted code in question

    – Shota Noniashvili
    Nov 14 '18 at 14:57











  • The sample not working on android 6.0.0 API 23, and even on 7.1.0 API 25

    – Shota Noniashvili
    Nov 15 '18 at 6:16











  • You must be able to see in the ReadMe that the sample uses apis those are available only in API level 26 and above (Android 8) so would not work on lower versions. But still the procedure is same.

    – Manoj
    Nov 15 '18 at 9:13






  • 1





    Do you have repo? Because it works on my end. I periodically connect to my server and update data, I extend from android.app.Service

    – Manoj
    Nov 15 '18 at 10:38







1




1





If the service is stopped, it will be restarted automatically. Are you facing this issue in any specific version of Android? Did you follow the sample here?

– Manoj
Nov 14 '18 at 14:48





If the service is stopped, it will be restarted automatically. Are you facing this issue in any specific version of Android? Did you follow the sample here?

– Manoj
Nov 14 '18 at 14:48













I will try that later. Android version is 6.0.0. API 23. emulator: Genymotion. I have edited and pasted code in question

– Shota Noniashvili
Nov 14 '18 at 14:57





I will try that later. Android version is 6.0.0. API 23. emulator: Genymotion. I have edited and pasted code in question

– Shota Noniashvili
Nov 14 '18 at 14:57













The sample not working on android 6.0.0 API 23, and even on 7.1.0 API 25

– Shota Noniashvili
Nov 15 '18 at 6:16





The sample not working on android 6.0.0 API 23, and even on 7.1.0 API 25

– Shota Noniashvili
Nov 15 '18 at 6:16













You must be able to see in the ReadMe that the sample uses apis those are available only in API level 26 and above (Android 8) so would not work on lower versions. But still the procedure is same.

– Manoj
Nov 15 '18 at 9:13





You must be able to see in the ReadMe that the sample uses apis those are available only in API level 26 and above (Android 8) so would not work on lower versions. But still the procedure is same.

– Manoj
Nov 15 '18 at 9:13




1




1





Do you have repo? Because it works on my end. I periodically connect to my server and update data, I extend from android.app.Service

– Manoj
Nov 15 '18 at 10:38





Do you have repo? Because it works on my end. I periodically connect to my server and update data, I extend from android.app.Service

– Manoj
Nov 15 '18 at 10:38












1 Answer
1






active

oldest

votes


















1














So here is how I did my service,



 import * as timer from "timer";

@JavaProxy("com.nativescript.DataSyncService")
class DataSyncService extends android.app.Service

private timerId: number;

onBind(): android.os.IBinder
return null;


onCreate(): void
super.onCreate();

if (!this.timerId)
this.timerId = timer.setInterval(()=>
// Run this every 4 hrs
, (1000 * 60 * 60 * 4));



onStartCommand(intent: android.content.Intent, flags: number, startId: number): number
return android.app.Service.START_STICKY;


onDestroy(): void
super.onDestroy();
timer.clearInterval(this.timerId);




Returning START_STICKY from onStartCommand makes the system to restart the service when the process (app) is killed.






share|improve this answer























  • Thank you very much. It works now

    – Shota Noniashvili
    Nov 15 '18 at 14:01










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53302560%2fnativescript-background-service-stops-working-after-app-closes%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














So here is how I did my service,



 import * as timer from "timer";

@JavaProxy("com.nativescript.DataSyncService")
class DataSyncService extends android.app.Service

private timerId: number;

onBind(): android.os.IBinder
return null;


onCreate(): void
super.onCreate();

if (!this.timerId)
this.timerId = timer.setInterval(()=>
// Run this every 4 hrs
, (1000 * 60 * 60 * 4));



onStartCommand(intent: android.content.Intent, flags: number, startId: number): number
return android.app.Service.START_STICKY;


onDestroy(): void
super.onDestroy();
timer.clearInterval(this.timerId);




Returning START_STICKY from onStartCommand makes the system to restart the service when the process (app) is killed.






share|improve this answer























  • Thank you very much. It works now

    – Shota Noniashvili
    Nov 15 '18 at 14:01















1














So here is how I did my service,



 import * as timer from "timer";

@JavaProxy("com.nativescript.DataSyncService")
class DataSyncService extends android.app.Service

private timerId: number;

onBind(): android.os.IBinder
return null;


onCreate(): void
super.onCreate();

if (!this.timerId)
this.timerId = timer.setInterval(()=>
// Run this every 4 hrs
, (1000 * 60 * 60 * 4));



onStartCommand(intent: android.content.Intent, flags: number, startId: number): number
return android.app.Service.START_STICKY;


onDestroy(): void
super.onDestroy();
timer.clearInterval(this.timerId);




Returning START_STICKY from onStartCommand makes the system to restart the service when the process (app) is killed.






share|improve this answer























  • Thank you very much. It works now

    – Shota Noniashvili
    Nov 15 '18 at 14:01













1












1








1







So here is how I did my service,



 import * as timer from "timer";

@JavaProxy("com.nativescript.DataSyncService")
class DataSyncService extends android.app.Service

private timerId: number;

onBind(): android.os.IBinder
return null;


onCreate(): void
super.onCreate();

if (!this.timerId)
this.timerId = timer.setInterval(()=>
// Run this every 4 hrs
, (1000 * 60 * 60 * 4));



onStartCommand(intent: android.content.Intent, flags: number, startId: number): number
return android.app.Service.START_STICKY;


onDestroy(): void
super.onDestroy();
timer.clearInterval(this.timerId);




Returning START_STICKY from onStartCommand makes the system to restart the service when the process (app) is killed.






share|improve this answer













So here is how I did my service,



 import * as timer from "timer";

@JavaProxy("com.nativescript.DataSyncService")
class DataSyncService extends android.app.Service

private timerId: number;

onBind(): android.os.IBinder
return null;


onCreate(): void
super.onCreate();

if (!this.timerId)
this.timerId = timer.setInterval(()=>
// Run this every 4 hrs
, (1000 * 60 * 60 * 4));



onStartCommand(intent: android.content.Intent, flags: number, startId: number): number
return android.app.Service.START_STICKY;


onDestroy(): void
super.onDestroy();
timer.clearInterval(this.timerId);




Returning START_STICKY from onStartCommand makes the system to restart the service when the process (app) is killed.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 15 '18 at 13:04









ManojManoj

6,1882922




6,1882922












  • Thank you very much. It works now

    – Shota Noniashvili
    Nov 15 '18 at 14:01

















  • Thank you very much. It works now

    – Shota Noniashvili
    Nov 15 '18 at 14:01
















Thank you very much. It works now

– Shota Noniashvili
Nov 15 '18 at 14:01





Thank you very much. It works now

– Shota Noniashvili
Nov 15 '18 at 14:01



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53302560%2fnativescript-background-service-stops-working-after-app-closes%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

Museum of Modern and Contemporary Art of Trento and Rovereto