set Alarm by time and repeat - Android android.app

Android examples for android.app:Alarm Set up

Description

set Alarm by time and repeat

Demo Code

import java.util.Calendar;

import com.android.willen.autoshutdown.server.AlarmReceiver;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class Main{

    public static void setAlarm(Context context, String time,
            int requestCode, boolean repeat) {
        Calendar c = Calendar.getInstance();
        int hour = Integer.valueOf(time.substring(0, 2));
        int minute = Integer.valueOf(time.substring(3, 5));
        Calendar now = Calendar.getInstance();

        c.set(Calendar.YEAR, now.get(Calendar.YEAR));
        c.set(Calendar.MONTH, now.get(Calendar.MONTH));
        c.set(Calendar.DAY_OF_MONTH, now.get(Calendar.DAY_OF_MONTH));
        c.set(Calendar.HOUR_OF_DAY, hour);
        c.set(Calendar.MINUTE, minute);
        c.set(Calendar.SECOND, 0);

        Intent intent1 = new Intent(context, Main.class);
        PendingIntent pi = PendingIntent.getBroadcast(context, requestCode,
                intent1, requestCode);//from   w w w  .j  a v  a  2 s.c o  m
        AlarmManager am = (AlarmManager) context.getSystemService("alarm");
        if (repeat) {
            long repeatingTime = 86400 * 1000;
            am.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(),
                    repeatingTime, pi);
        } else {
            am.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pi);
        }
        Log.i("setAlarm", time);
    }
    public static void setAlarm(Context context, long time, int requestCode) {
    Intent intent1 = new Intent(context, Main.class);
        PendingIntent pi = PendingIntent.getBroadcast(context, requestCode,
                intent1, requestCode);
        AlarmManager am = (AlarmManager) context.getSystemService("alarm");
        am.set(AlarmManager.RTC_WAKEUP, time, pi);

        Log.i("setAlarm", time + "");
    }

}

Related Tutorials