set Next Alarm - Android Android OS

Android examples for Android OS:Alarm Set Up

Description

set Next Alarm

Demo Code


import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Locale;
import android.annotation.SuppressLint;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;

public class Main{
    public static final String EXTRA_MILLIS = "timeInMillis";
    @SuppressLint("NewApi")
    public static void setNextAlarm(Context context, long millis) {

        //Get intent.
        Intent i = new Intent(context, TimendromeService.class);

        //Save trigger time for intent service (Problem with desync).
        i.putExtra(TimendromeUtils.EXTRA_MILLIS, millis);

        //Get pending intent.
        PendingIntent pi = PendingIntent.getService(context, 0, i,
                PendingIntent.FLAG_CANCEL_CURRENT);

        //Get alarm manager.
        AlarmManager am = (AlarmManager) context
                .getSystemService(Context.ALARM_SERVICE);

        //Call intent service.
        if (Build.VERSION.SDK_INT < 19) {
            am.set(AlarmManager.RTC_WAKEUP, millis, pi);
        } else {//from  w ww  .j a v  a 2 s .c  o m
            am.setExact(AlarmManager.RTC_WAKEUP, millis, pi);
        }
    }
}

Related Tutorials