NativeScript background service stops working after app closes
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) => {
console.log("Background watchLocation error: " + (e.message || e));
},
{
desiredAccuracy: Accuracy.high,
updateDistance: 0.1,
updateTime: 3000,
minimumUpdateTime: 100
});
}, (e) => {
console.log("Background enableLocationRequest error: " + (e.message || 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
|
show 3 more comments
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) => {
console.log("Background watchLocation error: " + (e.message || e));
},
{
desiredAccuracy: Accuracy.high,
updateDistance: 0.1,
updateTime: 3000,
minimumUpdateTime: 100
});
}, (e) => {
console.log("Background enableLocationRequest error: " + (e.message || 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
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 fromandroid.app.Service
– Manoj
Nov 15 '18 at 10:38
|
show 3 more comments
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) => {
console.log("Background watchLocation error: " + (e.message || e));
},
{
desiredAccuracy: Accuracy.high,
updateDistance: 0.1,
updateTime: 3000,
minimumUpdateTime: 100
});
}, (e) => {
console.log("Background enableLocationRequest error: " + (e.message || 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
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) => {
console.log("Background watchLocation error: " + (e.message || e));
},
{
desiredAccuracy: Accuracy.high,
updateDistance: 0.1,
updateTime: 3000,
minimumUpdateTime: 100
});
}, (e) => {
console.log("Background enableLocationRequest error: " + (e.message || 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
android nativescript angular2-nativescript android-broadcastreceiver background-service
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 fromandroid.app.Service
– Manoj
Nov 15 '18 at 10:38
|
show 3 more comments
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 fromandroid.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
|
show 3 more comments
1 Answer
1
active
oldest
votes
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.
Thank you very much. It works now
– Shota Noniashvili
Nov 15 '18 at 14:01
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
Thank you very much. It works now
– Shota Noniashvili
Nov 15 '18 at 14:01
add a comment |
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.
Thank you very much. It works now
– Shota Noniashvili
Nov 15 '18 at 14:01
add a comment |
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.
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.
answered Nov 15 '18 at 13:04
ManojManoj
6,1232922
6,1232922
Thank you very much. It works now
– Shota Noniashvili
Nov 15 '18 at 14:01
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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