Reputation: 47
I am trying to send a message (SMS as seen in the code) after a certain amount of time passed. But i don't think the CountDownTimer is working properly because it sends the first message as soon as the notification is shown.
Here is the code below
@SuppressWarnings("static-access")
@Override
public void onStart(Intent intent, int startId)
{
super.onStart(intent, startId);
mManager = (NotificationManager) this.getApplicationContext().getSystemService(this.getApplicationContext().NOTIFICATION_SERVICE);
Intent intent1 = new Intent(this.getApplicationContext(),MainActivity2Activity.class);
Notification notification = new Notification(R.mipmap.ic_launcher,"This is a test message!", System.currentTimeMillis());
intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP| Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingNotificationIntent = PendingIntent.getActivity( this.getApplicationContext(),0, intent1,PendingIntent.FLAG_UPDATE_CURRENT);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.setLatestEventInfo(this.getApplicationContext(), "AlarmManagerDemo", "This is a test message!", pendingNotificationIntent);
mManager.notify(0, notification);
CountDownTimer waitTimer;
waitTimer = new CountDownTimer(180000, 60000) {
public void onTick(long millisUntilFinished) {
sendmessage();
}
public void onFinish() {
//After 60000 milliseconds (60 sec) finish current
//if you would like to execute something when time finishes
}
}.start();
}
So how can I make the timer wait properly ? Because as I said it sends the first message as soon as the notification is called.
Note: And if the user taps on the notification it opens an activity which is supposed to kill the CountDownTimer but I couldn't achive that. How can do that ? I couldn't find anything about it.
Upvotes: 2
Views: 652
Reputation: 1369
You can implement with the below code,give intervalTime as your waiting time in millisecs.
Timer waitingTimer = new Timer();
waitingTimer.schedule(new TimerTask() {
@Override
public void run() {
sendMessage();
waitingTimer.cancel();
}
}, intervalTime, 5000);
Upvotes: 1
Reputation: 4656
Observing the CountDownTimer
reveals that first time onTick()
is called immediately after calling onStart()
and after that it is called after specified intervals as specified.
Secondly the first parameter passed 180000
tells the timer to call onFinish() after 180000
mills and call onTick() after 60000
millis in between starting immediately. If you want to restrict it to not send message the first time than you can do this simple check like this:
CountDownTimer waitTimer;
waitTimer = new CountDownTimer(180000, 60000) {
boolean firstTime = true;
public void onTick(long millisUntilFinished) {
if (firstTime) {
firstTime = false;
return;
}
sendmessage();
}
}
public void onFinish() {
//After 180000 milliseconds finish current
//if you would like to execute something when time finishes
}
}.start();
OR
You can also use TimerTask to perform the same operation:
Timer waitingTimer = new Timer();
waitingTimer.schedule(new TimerTask() {
@Override
public void run() {
sendMessage();
// Add a base condition here to cancel the task if needed..
}
}, 60000, 180000);
Upvotes: 1
Reputation: 5634
You can use Timer and TimerTask for this. You can set a delay to run and for repeating you can set a time after which it should execute again.
Example:
Timer timer = null;
public void scheduleTimer (){
cancelTimer(); // Cancel scheduled timer if any.
timer = new Timer("message_sender");
timer.schedule(new TimerTask() {
@Override
public void run() {
//This timer will execute timertask with first delay of 2 seconds and repeat it every 3 seconds.
}
}, 2000, 3000);
}
//Method for cancelling any scheduled timer
private void cancelTimer(){
if (timer != null){
timer.cancel();
timer.purge();
timer = null;
}
}
If you want to do any UI level task then inside run() you would require to execute code by using runOnUiThread
Upvotes: 0