Example usage for android.content Context ALARM_SERVICE

List of usage examples for android.content Context ALARM_SERVICE

Introduction

In this page you can find the example usage for android.content Context ALARM_SERVICE.

Prototype

String ALARM_SERVICE

To view the source code for android.content Context ALARM_SERVICE.

Click Source Link

Document

Use with #getSystemService(String) to retrieve a android.app.AlarmManager for receiving intents at a time of your choosing.

Usage

From source file:Main.java

public static void setTimerReceiver(Context context, int repeat_time, Class<?> class_name) {
    AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    Intent intentReceiver = new Intent(context, class_name);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intentReceiver,
            PendingIntent.FLAG_CANCEL_CURRENT);
    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.SECOND, repeat_time / 1000);

    // InexactRepeating allows Android to optimize the energy consumption
    alarm.setInexactRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), repeat_time, pendingIntent);
}

From source file:Main.java

public static void setAlarmTime(Context context, long timeInMillis, Intent intent) {
    AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    PendingIntent sender = PendingIntent.getBroadcast(context, intent.getIntExtra("id", 0), intent,
            PendingIntent.FLAG_CANCEL_CURRENT);
    int interval = (int) intent.getLongExtra("intervalMillis", 0);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        am.setWindow(AlarmManager.RTC_WAKEUP, timeInMillis, interval, sender);
    }/*from w w w . ja v  a  2  s .c  o m*/
}

From source file:Main.java

@TargetApi(Build.VERSION_CODES.CUPCAKE)
public static void stopAlarmIntent(Context context, PendingIntent pendingIntent) {
    AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    manager.cancel(pendingIntent);/*ww w  .j  a  v a  2s .  co  m*/
}

From source file:Main.java

@SuppressLint("NewApi")
private static void setAlarm(Context context, Calendar calendar, PendingIntent pIntent) {
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
        alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pIntent);
    } else {// w  w w .  ja va 2s  .co m
        alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pIntent);
    }
}

From source file:Main.java

public static boolean setAlarmByBC(Context context, Class<?> bc_class, int period) {
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    if (alarmManager == null)
        return false;
    long triggerAtTime = SystemClock.elapsedRealtime() + period;
    Intent i = new Intent(context, bc_class);
    PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
    alarmManager.set(AlarmManager.ELAPSED_REALTIME, triggerAtTime, pi);
    return true;/*ww  w.  j  a  va2s .  c  o  m*/
}

From source file:Main.java

@TargetApi(Build.VERSION_CODES.CUPCAKE)
public static void startAlarmIntent(Context context, int triggerAtMillis, PendingIntent pendingIntent) {
    AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    manager.set(AlarmManager.RTC_WAKEUP, triggerAtMillis, pendingIntent);
}

From source file:Main.java

@TargetApi(Build.VERSION_CODES.KITKAT)
public static void setAlarmExact(Context context, int type, long triggerAtMillis, PendingIntent pendingIntent) {
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        alarmManager.setExact(type, triggerAtMillis, pendingIntent);
    } else {//from  ww  w  .  java2  s.  c  om
        alarmManager.set(type, triggerAtMillis, pendingIntent);
    }
}

From source file:Main.java

public static void canalAlarm(Context context, String action) {
    Intent intent = new Intent(action);
    PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
    AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    am.cancel(pi);// w w w. ja v  a2 s  . c o m
}

From source file:Main.java

public static void cancel(Context context) {
    AlarmManager mAlarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    PendingIntent pintent = PendingIntent.getBroadcast(context, ALARM_REQUEST, new Intent(INTENT_FILTER_NAME),
            PendingIntent.FLAG_CANCEL_CURRENT);
    mAlarm.cancel(pintent);/*from w  w w.j a  v  a2 s . c  o m*/
}

From source file:Main.java

public static void setAlarm(Context context, Intent intent, int notificationId, Calendar calendar) {
    intent.putExtra("NOTIFICATION_ID", notificationId);
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, notificationId, intent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                pendingIntent);//from   ww  w.j  a va 2s . co  m
    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
    } else {
        alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
    }
}