Я пытаюсь отправить сообщение (SMS, как видно из кода) по прошествии определенного времени. Но я не думаю, что CountDownTimer работает должным образом, потому что он отправляет первое сообщение, как только отображается уведомление.
Вот код ниже
@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();
}
Итак, как я могу заставить таймер ждать правильно? Потому что, как я уже сказал, он отправляет первое сообщение, как только вызывается уведомление.
Примечание. И если пользователь нажимает на уведомление, он открывает действие, которое должно убить CountDownTimer, но я не смог этого добиться. Как это можно сделать? Я ничего не мог найти об этом.