Android - how to set an alarm to a specific date
package your.pack.name;
import java.util.Calendar;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
public class AlarmActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
cal.clear();
cal.set(2012,2,8,18,16);
AlarmManager alarmMgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
// cal.add(Calendar.SECOND, 5);
alarmMgr.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);
}
}
Is it possible to set a specific date with the Alarm Clock?
You cannot set the an alarm for a specific date with an AlarmClock
provider.
AlarmClock.EXTRA_DAYS
is for repeating alarms. You could use something like Calendar.SUNDAY
and it would ring every Sunday.
If you want more control over an alarm, you have to program it yourself. Look into the AlarmManager
class. It allows you to schedule your application to be run at some point in the future.
How to set android alarm to trigger until an specific date
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 14);
calendar.set(Calendar.MINUTE, 30);
int weekInMillis = 7 * 24 * 60 * 60 * 1000;
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
weekInMillis, PendingIntent.getBroadcast(context, 0, new Intent(context, ReminderAlarmWakefulBroadcastReceiver.class), PendingIntent.FLAG_UPDATE_CURRENT));
Above code snippet sets an alarm for 2:30 PM that repeats itself every week. Tweak calendar
for varying the time at which the alarm goes off. For example, the coming Monday.
When the alarm goes off, it sends a broadcast which will be received by ReminderWakefulBroadcastReceiver
, a custom receiver containing the code that you want to run every Monday at 2:30 PM. This code should also check whether it is time to cancel the alarm and if it is, the following code cancels it:
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(PendingIntent.getBroadcast(context, 0, new Intent(context, ReminderAlarmWakefulBroadcastReceiver.class));
References:
AlarmManager, Scheduling Repeating Alarms, PendingIntent
Android alarm setting with specific date
You should call public void setRepeating (int type, long triggerAtTime, long interval, PendingIntent operation)
(see here) to repeat the alarm. For example, you want to fire the alarm on 9:00 am every day, you can do :
Calendar c=Calendar.getInstance();
c.set(Calendar.HOUR_OF_DAY, 9);
c.set(Calendar.MINUTE, 0);
setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), AlarmManager.INTERVAL_DAY,pendingIntent);
Also,set the last parameter to 0 when initilazing the PendingIntent.
PendingIntent pendingIntent = PendingIntent.getBroadcast(fridgeDetails.this,
0, myIntent, 0);
How to set an alarm at a specific date at any chosen time?
Use BroadcastReceiver.
public class MyBroadcastReceiver extends BroadcastReceiver {
MediaPlayer mp;
@Override
public void onReceive(Context context, Intent intent) {
mp=MediaPlayer.create(context, R.raw.alrm );
mp.start();
Toast.makeText(context, "Alarm....", Toast.LENGTH_LONG).show();
}
}
Start Alarm-
// i = int time from calander.
int i = 12;
Intent intent = new Intent(this, MyBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
this.getApplicationContext(), 234324243, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()
+ (i * 1000), pendingIntent);
Toast.makeText(this, "Alarm set in " + i + " seconds",Toast.LENGTH_LONG).show();
Related Topics
Pop the Fragment Backstack Without Playing the Pop-Animation
How to Get Chrome Browsing History/Bookmarks in Our Android App
How to Download a File from a Server and Save It in Specific Folder in Sd Card in Android
Is Arm64-V8A Compatible with Armeabi-V7A
Android Repeat Action on Pressing and Holding a Button
Savedinstancestate Is Always Null in Fragment
How to Download a PDF File in Android
Video Compression on Android Using New Mediacodec Library
Gradle Flavors for Android with Custom Source Sets - What Should the Gradle Files Look Like
Android Sax Parser Not Getting Full Text from Between Tags
Using the New "Manifestmerger" Property in Android
Setsupportactionbar Toolbar Cannot Be Applied to (Android.Widget.Toolbar) Error
Button in Custom Android Toast
How to Handle System_Alert_Window Permission Not Being Auto-Granted on Some Pre-Marshmallow Devices