简体   繁体   中英

Trouble in Java TimerTask Interval

Sorry for my bad english.

I'm implementing Java TimerTask when creating an Android application. The code isn't to complicated. When it hit the interval, it will send an SMS & email.

I have some option with the sending SMS / email interval. 5 minutes, 15 minutes, 30 minutes, 1 hour, and 2 hour.

I tried using 5 minutes, and 15 minutes (I converted it into milis), no trouble occurred. It send sms and email every 5 / 15 minutes exactly. And I prove that i have no problem using timertask.

But, when I changed it to 30 minutes or more. my application cannot send sms / email. It looked like that the timer didn't work correctly.

Does it have any interval limitation on using Timer Task ??

this is my code snippet.

private void activateNotificationByInterval(int notificationInterval) {
    timerNotificationByInterval = new Timer();
    timerNotificationByInterval.schedule(new TimerTask() {
        @Override
        public void run() {
            // TODO Auto-generated method stub
            // coba
            // if (oldLatitude != childLatitude
            // && oldLongitude != childLongitude) {
            if ((enableSMS == true)) {
                sendSMS();
            }

            if ((enableEmail == true)) {
                GMailSender email = new GMailSender(sender, senderPassword);
                try {
                    email.sendMail(emailSubject + childsName, SMSMessage
                            + " " + childlLocation + "\nLatitude: "
                            + childLatitude + "\nLongitude: "
                            + childLongitude, sender, destination);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            mpv.changeLastLocation(childLatitude, childLongitude);
            // buat method untuk menangkap lokasi pertama
            oldLatitude = childLatitude;
            oldLongitude = childLongitude;
            // } else {
            // System.out.println("Lokasi Lama Berpengaruh");
            // }

        }
    }, 0, notificationInterval);
}

*notes : notificationInterval on milis (*60000)

can you tell me how to solved it ? thank you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM