Android Alarm Create addReminderAlarm(Context context, Date date, String group, boolean toast)

Here you can find the source of addReminderAlarm(Context context, Date date, String group, boolean toast)

Description

add Reminder Alarm

License

Open Source License

Declaration

public static void addReminderAlarm(Context context, Date date,
            String group, boolean toast) 

Method Source Code

//License from project: Open Source License 

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.SystemClock;
import android.widget.Toast;
import java.util.Date;
import uk.co.pilllogger.R;

public class Main{
    public static void addReminderAlarm(Context context, Date date,
            String group, boolean toast) {

        long difference = date.getTime() - new Date().getTime();

        Intent intent = new Intent(
                context.getString(R.string.intent_reminder));
        intent.putExtra(//from  w  ww . ja  v  a 2  s  .  c  om
                context.getString(R.string.intent_extra_notification_consumption_group),
                group);

        PendingIntent pi = PendingIntent.getBroadcast(context,
                date.hashCode(), intent, 0);
        AlarmManager am = (AlarmManager) (context
                .getSystemService(Context.ALARM_SERVICE));

        am.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                SystemClock.elapsedRealtime() + difference, pi);

        if (toast) {
            Toast.makeText(
                    context,
                    context.getString(R.string.add_consumption_reminder_toast_prefix)
                            + " " + DateHelper.getTime(context, date),
                    Toast.LENGTH_SHORT).show();
        }
    }
}

Related

  1. setAlarm( Class receiver, Calendar calendar, String action, Object... parameters)
  2. setAlarm(Context context, PendingIntent pIntent)
  3. setAlarm(Context context, Calendar calendar, PendingIntent pIntent)
  4. setAlarm(Context context, Calendar calendar, PendingIntent pIntent)