start Alarm by hour and minute - Android android.app

Android examples for android.app:Alarm start

Description

start Alarm by hour and minute

Demo Code

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import java.util.Calendar;

public class Main{

    public static void startAlarm(Context context, int type, int index,
            int hour, int minute, String action) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.set(Calendar.HOUR_OF_DAY, hour);
        calendar.set(Calendar.MINUTE, minute);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);

        if (calendar.getTimeInMillis() < System.currentTimeMillis()) {
            calendar.setTimeInMillis(System.currentTimeMillis()
                    + (24 * 60 * 60 * 1000));
            calendar.set(Calendar.HOUR_OF_DAY, hour);
            calendar.set(Calendar.MINUTE, minute);
            calendar.set(Calendar.SECOND, 0);
            calendar.set(Calendar.MILLISECOND, 0);
        }// www  .ja  va  2 s .  co  m

        Intent intent = new Intent(action);
        intent.putExtra("index", index);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
                index, intent, 0);
        AlarmManager am = (AlarmManager) context
                .getSystemService(Context.ALARM_SERVICE);
        am.set(type, calendar.getTimeInMillis(), pendingIntent);
    }
    public static void startAlarm(Context context, int type, int index,
            int interval, String action) {
        Intent intent = new Intent(action);
        intent.putExtra("index", index);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
                index, intent, 0);
        AlarmManager am = (AlarmManager) context
                .getSystemService(Context.ALARM_SERVICE);
        am.setRepeating(type, System.currentTimeMillis(), interval,
                pendingIntent);
    }

}

Related Tutorials