Nativescript如何启动服务?
我正在尝试为android(sdk 24)启动一个基本服务(基于此示例),但没有发生任何事情,也不确定我做错了什么,下面是我的代码:Nativescript如何启动服务?,nativescript,Nativescript,我正在尝试为android(sdk 24)启动一个基本服务(基于此示例),但没有发生任何事情,也不确定我做错了什么,下面是我的代码: function onNavigatingTo(args) { const page = args.object; const context = utils.ad.getApplicationContext(); if(platformModule.device.sdkVersion < '
function onNavigatingTo(args) {
const page = args.object;
const context = utils.ad.getApplicationContext();
if(platformModule.device.sdkVersion < '26'){
android.app.IntentService.extend("com.tns.notifications.BackgroundService", {
onHandleIntent(intent) {
console.log('service onHandleIntent');
},
onStartCommand(){
console.log('service onStartCommand');
}
});
let intent = new android.content.Intent(context, com.tns.notifications.BackgroundService.class);
intent.setFlags(android.content.Intent.FLAG_ACTIVITY_NEW_TASK);
context.startService(intent);
}
page.bindingContext = createViewModel();
}
导航到(args)上的函数{
const page=args.object;
const context=utils.ad.getApplicationContext();
if(platformModule.device.sdkVersion<'26'){
android.app.IntentService.extend(“com.tns.notifications.BackgroundService”{
onHandleIntent(意图){
console.log('service onHandleIntent');
},
onStartCommand(){
log('service onStartCommand');
}
});
让intent=newandroid.content.intent(context,com.tns.notifications.BackgroundService.class);
setFlags(android.content.intent.FLAG\u活动\u新任务);
上下文。startService(意图);
}
page.bindingContext=createViewModel();
}
在mainfest.xml中,我添加了“服务”标签:
“onHandleIntent”
或“onStartCommand”
在应用程序启动后从未触发,知道为什么吗?我发布到这个Nativescript地理位置的代码可能与此相关。谢谢David,我发现了问题,我在mainfest.xml“service”标记中输入了错误的服务名称。我输入的不是“com.tns.notifications.BackgroundService”,而是“com.nativescript.location.BackgroundService”,一旦我修复了它,服务就开始工作了。愚蠢的错误:(
<service android:name="com.nativescript.location.BackgroundService"
android:permission="android.permission.BIND_JOB_SERVICE"
android:enabled="true"
android:exported="false" >