1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
| const application = require("tns-core-modules/application");
const Timer = require("tns-core-modules/timer");
const Platform = require("tns-core-modules/platform");
let timerid = null;
if (application.android) {
if (Platform.device.sdkVersion < "26") {
(android.app.Service).extend("jp.inxray.scop.BackgroundService", {
onStartCommand: function (intent, flags, startId) {
this.super.onStartCommand(intent, flags, startId);
return android.app.Service.START_STICKY;
},
onCreate: function () {
startTimer(30);
console.log("background: start Service");
},
onBind: function (intent) {
console.log("background: on Bind Services");
},
onUnbind: function (intent) {
console.log('background: UnBind Service');
},
onDestroy: function () {
console.log('background: service onDestroy');
stopTimer();
}
});
} else {
(android.app).job.JobService.extend("jp.inxray.scop.BackgroundService26", {
onStartJob() {
startTimer(30);
console.log('background: service onStartJob');
return true;
},
onStopJob(jobParameters) {
console.log('background: service onStopJob');
this.jobFinished(jobParameters, false);
stopTimer();
return false;
},
});
}
}
/**
* 定期実行設定
* @param {number} interval 時間間隔(sec)
*/
function startTimer(interval) {
if(interval>0 && timerid==null){
timerid = Timer.setInterval(() => {
const randNumber = Math.random();
console.log(randNumber);
}, interval*1000);
console.log('worker: set timer to interval '+ interval*1000 + 'ms.');
}
}
/**
* 定期実行停止
*/
function stopTimer() {
if(timerid!=null){
Timer.clearInterval(timerid);
timerid = null;
console.log('background: stop timer.');
}
}
|