Frage

I'm trying to build an app where I'm building a countdown timer to a specific date and time. Now I've built the countdown timer using the android countdown timer class and it's working properly.

Now what I want to do is show a notification method in the onFinish() of the Countdown Timer saying that the event has arrived. I want the notification to show even if the app isn't running. Here is my code so far:

    new CountDownTimer(timer.getIntervalMillis(), 1000) {

        @Override
        public void onTick(long millisUntilFinished) {
            // TODO Auto-generated method stub
            int days = (int) ((millisUntilFinished / 1000) / 86400);
            int hours = (int) (((millisUntilFinished / 1000) - (days * 86400)) / 3600);
            int minutes = (int) (((millisUntilFinished / 1000)
                    - (days * 86400) - (hours * 3600)) / 60);
            int seconds = (int) ((millisUntilFinished / 1000) % 60);

            String countdown = String.format("%02d:%02d:%02d:%02d", days,
                    hours, minutes, seconds);
            countdownTimer.setText(countdown);
        }

        @Override
        public void onFinish() {
            // TODO Auto-generated method stub
            countdownBegins.setVisibility(View.GONE);
            countdownTimer.setText("SYMAGINE IS HERE!!");

        }
    }.start();

How do I show a notification when the countdown timer has finished. Even at the chance that the app may not be running. I've seen AlarmManager but somehow i didn't understand it. Help is appreciated with proper explanation since i'm kind of a newbie.

War es hilfreich?

Lösung

You should use android AlarmManager. When you Schedulle an alarm with it, it will trigger the event( it doesnt matter if your app is running or not, it will trigger the event ).

Since you want to show a notification i would suggest that you start a Service when the alarm triggers, and build your notification from the service.

Take a look at this: http://www.techrepublic.com/blog/software-engineer/use-androids-alarmmanager-to-schedule-an-event/

Here is an example : How to start Service using Alarm Manager in Android? --> it uses a repeating alarm but the logic is the same.

Try out a few examples and you will get the hold on of it, AlarmManager is actually very simple to use as you will see from the above examples.

Bellow example taken from : How to schedule a task using Alarm Manager

public void scheduleAlarm(View V)
{
    // time at which alarm will be scheduled here alarm is scheduled at 1 day from current time, 
    // we fetch  the current time in milliseconds and added 1 day time
    // i.e. 24*60*60*1000= 86,400,000   milliseconds in a day       
    Long time = new GregorianCalendar().getTimeInMillis()+24*60*60*1000;

    // create an Intent and set the class which will execute when Alarm triggers, here we have
    // given AlarmReciever in the Intent, the onRecieve() method of this class will execute when

    Intent intentAlarm = new Intent(this, AlarmReciever.class);

    //Get the Alarm Service
    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

    //set the alarm for particular time
    alarmManager.set(AlarmManager.RTC_WAKEUP,time, PendingIntent.getBroadcast(this,1,  intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT));
    Toast.makeText(this, "Alarm Scheduled for Tommrrow", Toast.LENGTH_LONG).show();
}

AlarmReciever Class

public class AlarmReciever extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        // TODO Auto-generated method stub

        // Your Code When Alarm will trigger              

    }
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top