我在谷歌搜索了几个小时,但我只得到了 3 页这个问题,而且没有一个对我有帮助。
我在 tns:5.3.0
TestJobService.js
android.app.job.JobService.extend("org.nativescipt.ITPalert.TestJobService", {
onStartJob() {
console.log("Job execution ...");
// here you can do whatever you want
//this.jobFinished(params, true); //this ends the job if successful, if not `return false;`
return true;
},
onStopJob() {
console.log("Stopping job ...");
return true; //returning true makes the task reschedule
},
});
主页.vue
startBackground() {
const context = utils.ad.getApplicationContext();
const component = new android.content.ComponentName(context, org.nativescipt.ITPalert.TestJobService.class);
const builder = new android.app.job.JobInfo.Builder(1, component);
builder.setRequiredNetworkType(android.app.job.JobInfo.NETWORK_TYPE_ANY);
builder.setMinimumLatency(1000 * 60)
builder.setOverrideDeadline(1000 * 100)
builder.setPersisted(true)
const jobScheduler = context.getSystemService(android.content.Context.JOB_SCHEDULER_SERVICE);
const service = jobScheduler.schedule(builder.build());
console.log(`Job Scheduled: ${jobScheduler.schedule(builder.build())}`);
},
在我的 AndroidManifest.xml 我有:
<service android:name="org.nativescipt.ITPalert.TestJobService"
android:permission="android.permission.BIND_JOB_SERVICE"
android:enabled="true"
android:exported="false" />
我在 webpack.config.js 中注册了作业:
const appComponents = [
"tns-core-modules/ui/frame",
"tns-core-modules/ui/frame/activity",
resolve(__dirname, "app/Jobs/TestJobService.js"),
];
我得到了这个错误:
System.err: com.tns.NativeScriptException:
System.err: Calling js method onClick failed
System.err:
System.err: Error: java.lang.IllegalArgumentException: No such service ComponentInfo{org.nativescript.ITPalert/org.nativescipt.ITPalert.TestJobService}
System.err: android.os.Parcel.readException(Parcel.java:1963)
System.err: android.os.Parcel.readException(Parcel.java:1905)
System.err: android.app.job.IJobScheduler$Stub$Proxy.schedule(IJobScheduler.java:180)
System.err: android.app.JobSchedulerImpl.schedule(JobSchedulerImpl.java:44)
System.err: com.tns.Runtime.callJSMethodNative(Native Method)
System.err: com.tns.Runtime.dispatchCallJSMethodNative(Runtime.java:1203)
System.err: com.tns.Runtime.callJSMethodImpl(Runtime.java:1083)
System.err: com.tns.Runtime.callJSMethod(Runtime.java:1070)
System.err: com.tns.Runtime.callJSMethod(Runtime.java:1050)
System.err: com.tns.Runtime.callJSMethod(Runtime.java:1042)
System.err: com.tns.gen.java.lang.Object_vendor_30697_26_ClickListenerImpl.onClick(Object_vendor_30697_26_ClickListenerImpl.java:17)
System.err: android.view.View.performClick(View.java:6896)
我用--bundle 和--hmr 构建了我的项目。