javascript - Js类扩展返回太快
问题描述
我正在使用 javaScript 在 nativeScript 中构建任务调度程序。
JobScheduler 调用需要执行的 JobService。
但是 JobService 返回太快。
你可以看出我使用nearby()
了工作正常的异步函数,但我不能用nearby()
大括号包裹整个脚本。
所以我需要一些方法来延迟返回语句,直到我拥有所有数据。
调度器.js:
function scheduleJob(context) {
// Create a component from the JobService that should be triggered
var component = new android.content.ComponentName(context, com.tns.notifications.MyJobService.class);
// Set the id of the job to something meaningful for you
const builder = new android.app.job.JobInfo.Builder(1, component);
// Optional: Set how often the task should be triggered. The minimum is 15min.
builder.setPeriodic(15 * 60 * 1000);
// Optional: Set additional requirements under what conditions your job should be triggered
// builder.setRequiresCharging(true);
builder.setOverrideDeadline(0);
const jobScheduler = context.getSystemService(android.content.Context.JOB_SCHEDULER_SERVICE);
console.log("Job Scheduled: " + jobScheduler.schedule(builder.build()));
}
module.exports.scheduleJob = scheduleJob;
myJobService.js
android.app.job.JobService.extend("com.tns.notifications.MyJobService", {
onStartJob: function(params) {
console.log("Job execution ...");
// Do something useful here, fetch data and show notification for example
var utils = require("utils/utils");
var context = utils.ad.getApplicationContext();
var res;
var geoService = require("./geocalc");
var PointModell = require("./PointModell")
nearby();
async function nearby(){
res=await geoService.geocalc();
console.log("res",res);
}
var builder = new android.app.Notification.Builder(context);
console.log("setting notification head and body")
builder.setContentTitle("you are in .......")
.setAutoCancel(true)
.setColor(android.R.color.holo_purple)//getResources().getColor(R.color.colorAccent))
.setContentText("This notification has been triggered ")
.setVibrate([100, 200, 100])
.setSmallIcon(android.R.drawable.btn_star_big_on);
// will open main NativeScript activity when the notification is pressed
var mainIntent = new android.content.Intent(context, com.tns.NativeScriptActivity.class);
var mNotificationManager = context.getSystemService(android.content.Context.NOTIFICATION_SERVICE);
// The id of the channel.
const channelId = "my_channel_01";
// The user-visible name of the channel.
const name = "Channel name";
// The user-visible description of the channel.
const description = "Channel description";
const importance = android.app.NotificationManager.IMPORTANCE_LOW;
if (android.os.Build.VERSION.SDK_INT >= 26) {
console.log("apilevel is good",android.os.Build.VERSION.SDK_INT)
}
const mChannel = new android.app.NotificationChannel(channelId, name,importance);
// const mChannel = new android.app.NotificationChannel(channelId, name,importance);
// Configure the notification channel.
mChannel.setDescription(description);
mChannel.enableLights(true);
// Sets the notification light color for notifications posted to this
// channel, if the device supports this feature.
mChannel.setLightColor(android.graphics.Color.RED);
mChannel.enableVibration(true);
mNotificationManager.createNotificationChannel(mChannel);
builder.setChannelId(channelId);
mNotificationManager.notify(1, builder.build());
console.log("returning job ...");
return false
},
onStopJob: function() {
console.log("Stopping job ...");
}
});
解决方案
即使使用 async/await,也不能延迟返回值。我认为你应该在完成所有数据后返回true
并调用jobFinished方法。
推荐阅读
- jquery - 如何使它成为for循环
- javascript - 使用 jquery 设置 div 数据属性
- javascript - 我如何将函数的 fileId 用于其他函数?
- node.js - 自动化手动 git 和构建过程
- python - 尝试在 python 中安装视觉文件时出错
- python - 尝试导入嵌套模块时 Python 中的 ModuleNotFound 错误
- asp.net-core - Kestrel / IIS 托管与 ASP.Net Core 2.2 / 3.0 应用程序切换
- jquery - 剃刀控制器值为空
- java - IntelliJ IDEA - 直接在 IDE 中运行时,Java 工具 premain 会被调用两次
- android - android中的分段圆形进度条