【发布时间】:2019-08-06 04:38:19
【问题描述】:
我正在尝试在 Android 上的 nativescript 中制作专门的虚拟助手。为此,我需要一个后台服务不断监听唤醒词。现在我设法提供了一项服务,但是在使用 START_STICKY 杀死应用程序后重新启动它时,它会因com.tns.NativeScriptException: Failed to create JavaScript extend wrapper for class 'tk/ozymandias/ServiceTest/Service' 而崩溃。
这里是服务:
@JavaProxy("tk.ozymandias.ServiceTest.Service")
class Service extends android.app.Service {
private timerId: number;
onBind():android.os.IBinder {
return null;
}
onCreate(): void {
super.onCreate();
if (!this.timerId) {
this.timerId = setInterval(() => {
console.log("PING");
}, 1000);
}
}
onStartCommand(intent: android.content.Intent, flags: number, startId: number) {
return android.app.Service.START_STICKY;
}
onDestroy(): void {
super.onDestroy();
clearInterval(this.timerId);
}
}
这是堆栈跟踪:
An uncaught Exception occurred on "main" thread.
Unable to create service tk.ozymandias.ServiceTest.Service: com.tns.NativeScriptException: Failed to create JavaScript extend wrapper for class 'tk/ozymandias/ServiceTest/Service'
StackTrace:
java.lang.RuntimeException: Unable to create service tk.ozymandias.ServiceTest.Service: com.tns.NativeScriptException: Failed to create JavaScript extend wrapper for class 'tk/ozymandias/ServiceTest/Service'
at android.app.ActivityThread.handleCreateService(ActivityThread.java:3349)
at android.app.ActivityThread.-wrap4(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1677)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:440)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:108)
Caused by: com.tns.NativeScriptException: Failed to create JavaScript extend wrapper for class 'tk/ozymandias/ServiceTest/Service'
at com.tns.Runtime.createJSInstanceNative(Native Method)
at com.tns.Runtime.createJSInstance(Runtime.java:778)
at com.tns.Runtime.initInstance(Runtime.java:751)
at tk.ozymandias.ServiceTest.Service.onCreate(Service.java:29)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:3339)
... 9 more
您可以看到最小的示例项目here。 要触发错误,您需要实际终止应用程序,使用后退按钮退出就可以了。
【问题讨论】:
标签: android typescript nativescript