start Alarm - Android android.app

Android examples for android.app:Alarm start

Description

start Alarm

Demo Code

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

public class Main{

    private static final int ALARM_ID = 0;
    public static void startAlarm(final Context context,
            final int intervalMillis) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MILLISECOND, intervalMillis);

        Intent alarmIntent = new Intent(
                AppWidgetManager.ACTION_APPWIDGET_UPDATE);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
                ALARM_ID, alarmIntent, PendingIntent.FLAG_CANCEL_CURRENT);

        AlarmManager alarmManager = (AlarmManager) context
                .getSystemService(Context.ALARM_SERVICE);
        // RTC does not wake the device up
        alarmManager.setRepeating(AlarmManager.RTC,
                calendar.getTimeInMillis(), intervalMillis, pendingIntent);
    }/* ww  w. j  a  va  2 s . c  o  m*/

}

Related Tutorials