应用程序关闭后 NativeScript 后台服务停止工作
NativeScript background service stops working after app closes
我的 nativescript 后台服务有问题。我想编写带有后台服务的应用程序。该服务必须始终 运行 在后台,即使应用程序将停止并关闭。
我用过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但结果是一样的:
当应用程序关闭时,该服务也停止工作。
我想编写 gps 追踪器后台服务,这就是为什么我需要始终在后台 运行ning 服务。
服务代码:
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));
});
}
}
开始服务
const context = application.android.context;
const intent = new android.content.Intent();
intent.setClassName(context, "org.nativescript.PresleyApp.Test");
context.startService(intent);
这就是我的服务方式,
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);
}
}
从 onStartCommand
返回 START_STICKY 使系统在进程 (app) 被终止时重新启动服务。
我的 nativescript 后台服务有问题。我想编写带有后台服务的应用程序。该服务必须始终 运行 在后台,即使应用程序将停止并关闭。
我用过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但结果是一样的: 当应用程序关闭时,该服务也停止工作。
我想编写 gps 追踪器后台服务,这就是为什么我需要始终在后台 运行ning 服务。
服务代码:
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));
});
}
}
开始服务
const context = application.android.context;
const intent = new android.content.Intent();
intent.setClassName(context, "org.nativescript.PresleyApp.Test");
context.startService(intent);
这就是我的服务方式,
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);
}
}
从 onStartCommand
返回 START_STICKY 使系统在进程 (app) 被终止时重新启动服务。