/*
This file is part of Bodhi Timer.
Bodhi Timer is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Bodhi Timer is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Bodhi Timer. If not, see <http://www.gnu.org/licenses/>.
*/
package org.yuttadhammo.BodhiTimer.Service;
import java.util.Calendar;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import org.yuttadhammo.BodhiTimer.TimerReceiver;
/**
* Set an alarm for the date passed into the constructor
* When the alarm is raised it will start the NotifyService
*
* This uses the android build in alarm manager *NOTE* if the phone is turned off this alarm will be cancelled
*
* This will run on it's own thread.
*
* @author paul.blundell
*/
public class AlarmTask implements Runnable{
// The date selected for the alarm
private final int time;
// The android system alarm manager
private final AlarmManager mAlarmMgr;
// Your context to retrieve the alarm manager from
private final Context context;
public AlarmTask(Context context, int time) {
this.context = context;
this.mAlarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
this.time = time;
}
@Override
public void run() {
Intent intent = new Intent(context, TimerReceiver.class);
intent.putExtra("SetTime", time);
PendingIntent mPendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
if (Build.VERSION.SDK_INT >= 19) {
mAlarmMgr.setExact(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + time, mPendingIntent);
}
else {
mAlarmMgr.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + time, mPendingIntent);
}
}
}