我正在使用javaScript在nativeScript中构建任务计划程序。
JobSchedualer调用需要执行的JobService。
但是JobService返回得太快。
您可以说我使用了异步功能nearby()
,该功能可以正常工作,但是我无法使用nearby()
大括号来包装整个脚本。
所以我需要某种方式来延迟return语句直到我拥有所有数据。
schedualer.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 ...");
}
});