add Alarm Broadcast - Android Android OS

Android examples for Android OS:Alarm Broadcast

Description

add Alarm Broadcast

Demo Code


//package com.java2s;
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);
    }/*  w  w  w .  ja v a 2s. c o m*/
}

Related Tutorials