set Alarm via BroadcastReceiver - Android Android OS

Android examples for Android OS:Alarm Set Up

Description

set Alarm via BroadcastReceiver

Demo Code


import java.util.ArrayList;
import java.util.HashSet;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.SystemClock;

public class Main{
    public static HashSet<PendingIntent> set = new HashSet<PendingIntent>();
    public static void setAlarm(Context ctx, BroadcastReceiver rcv,
            String action, int id, long time) {
        //Alarm Manager
        IntentFilter intentFilter = new IntentFilter(action);

        AlarmManager am = (AlarmManager) ctx
                .getSystemService(Context.ALARM_SERVICE);

        ctx.registerReceiver(rcv, intentFilter);

        Intent alarm = new Intent(action);
        PendingIntent sender = PendingIntent.getBroadcast(ctx, id, alarm,
                PendingIntent.FLAG_UPDATE_CURRENT);

        long current = SystemClock.elapsedRealtime();
        am.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, current + time, sender);
        set.add(sender);//from  w w w  .  j a va 2  s  . c o  m
    }
}

Related Tutorials