add Alarm Broadcast - Android android.app

Android examples for android.app:AlarmManager

Description

add Alarm Broadcast

Demo Code

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.SystemClock;

public class Main{

    public static void addAlarmBroadcast(Context context,
            long intervalTime, String action) {
        AlarmManager alarmManager = (AlarmManager) context
                .getApplicationContext().getSystemService(
                        Context.ALARM_SERVICE);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
                0, new Intent(action), PendingIntent.FLAG_UPDATE_CURRENT);
        long triggerTime = SystemClock.elapsedRealtime();// + intervalTime;
        alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME,
                triggerTime, intervalTime, pendingIntent);
    }/* ww  w.  j  a  v a 2 s . c o  m*/

}

Related Tutorials