I was having some problem when trying to set specific time to execute the AlarmManager. Here is the code where I schedule my Alarm:
public static void scheduleAlarms(Context context) {
Calendar cur_cal = new GregorianCalendar();
cur_cal.setTimeInMillis(System.currentTimeMillis());//set the current time and date for this calendar
Calendar cal = new GregorianCalendar();
cal.add(Calendar.DAY_OF_YEAR, cur_cal.get(Calendar.DAY_OF_YEAR));
cal.set(Calendar.HOUR_OF_DAY, 00);
cal.set(Calendar.MINUTE, 30);
cal.set(Calendar.SECOND, cur_cal.get(Calendar.SECOND));
cal.set(Calendar.MILLISECOND, cur_cal.get(Calendar.MILLISECOND));
cal.set(Calendar.DATE, cur_cal.get(Calendar.DATE));
cal.set(Calendar.MONTH, cur_cal.get(Calendar.MONTH));
AlarmManager mgr = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
Intent notificationIntent = new Intent(context, Alarm.class);
notificationCount = notificationCount + 1;
notificationIntent.putExtra("NotifyCount", notificationCount);
PendingIntent pi = PendingIntent.getBroadcast(context, 1,
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mgr.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pi);
ComponentName receiver = new ComponentName(context, BootReceiver.class);
PackageManager pm = context.getPackageManager();
pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
}
I am trying to set the alarm manager to run at 0030 if it meets the conditions in my if statement. However, the alarmManager will not send push-notification although some records did met the requirements. So I changed the codes to repeat every 2 minutes by:
mgr.setRepeating(AlarmManager.RTC_WAKEUP, System.getTimeInMillis(),
2*60*60, pi);
And it did send the push-notifications correctly. I wonder what's the mistake I made for the part when I tried to specified a time for it to execute.
Thanks in advance.
Related
I want to repeat the task at specific time like 9:45, 10:10
AlarmManager alarmMgr0 =
(AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent intent0 = new Intent(this,schedule.class);
PendingIntent pendingIntent0 = PendingIntent.getBroadcast(this, 0,
intent0, 0);
// Intent intent0 = new Intent(this, OldEntryRemover.class);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 32);
calendar.set(Calendar.SECOND, 0);
//set that timer as a RTC Wakeup to alarm manager object
alarmMgr0.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
pendingIntent0);
Try this:-
private AlarmManager alarmMgr;
private PendingIntent alarmIntent;
...
alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmReceiver.class);
alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
// Set the alarm to start at 8:30 a.m.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 8);
calendar.set(Calendar.MINUTE, 30);
// setRepeating() lets you specify a precise custom interval--in this case,
// 20 minutes.
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
1000 * 60 * 20, alarmIntent);
References:-https://developer.android.com/training/scheduling/alarms
I am new to android programming. I want to set different repeating alarms. I wrote a code, but it is not working. It is not generating any alarms. Please help me.
Here is my code
public void setAlarm(int hour, int min,String am_pm){
if(hour == 12 && am_pm.equals("am")){
hour=00;
}
if(hour != 12 && am_pm.equals("pm")){
hour=hour+12;
}
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, min);
Intent intent = new Intent(getBaseContext(), AlertReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), (int) System.currentTimeMillis(), intent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
}
For above I am passing setAlarm(8,30,"am")
I have some schedule in my app, and i need to do some task in specific time every day(in other time) example at 7:00 in Monday , at 12:00 in Tuesday etc. And schedule in others weeks of month can be different. How can i do this.
I think this will help you figure how to setup events happening at certain times and how to interact with them programmatically.
The main premise is that you use an AlarmManager to schedule your events:
Calendar cal = Calendar.getInstance();
Intent activate = new Intent(context, Alarm.class);
AlarmManager alarms ;
PendingIntent alarmIntent = PendingIntent.getBroadcast(context, 0, activate, 0);
alarms = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
cal.set(Calendar.HOUR_OF_DAY, hour);
cal.set(Calendar.MINUTE, minute);
cal.set(Calendar.SECOND, 00);
alarms.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), alarmIntent);
and you use a BroadcastReceiver to add functionality when that time is reached:
public class Alarm extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
MediaPlayer mp = MediaPlayer.create(context, R.raw.ferry_sound);
mp.start();
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "My Tag");
wl.acquire();
Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
long[] s = { 0, 100, 10, 500, 10, 100, 0, 500, 10, 100, 10, 500 };
vibrator.vibrate(s, -1);
}
}
You'll need to register your receiver in your manifest.xml
<Application ... >
...
<receiver android:process=":remote" android:name="Alarm" />
...
</Application>
I am having some problem with Alarm Manager in Android. I am setting the alarm manager to execute every midnight at 12am. Here are my codes:
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 0 );
notificationCount = notificationCount + 1;
AlarmManager mgr = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
Intent notificationIntent = new Intent(context,
ReminderAlarm.class);
notificationIntent.putExtra("RecurID", recurID);
notificationIntent.putExtra("RecurStartDate", _recurlist.get(position)
.getRecurringStartDate());
notificationIntent.putExtra("Date", dateFormat.format(new Date()));
notificationIntent.putExtra("Type", _recurlist.get(position).getRecurringType());
notificationIntent.putExtra("Amount", formatAmount);
notificationIntent.putExtra("NextPaymentDate", viewHolder.txt_ddate.getText());
notificationIntent.putExtra("NotifyCount", notificationCount);
PendingIntent pi = PendingIntent.getBroadcast(context,
notificationCount, notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
mgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, pi);
ComponentName receiver = new ComponentName(context, BootReceiver.class);
PackageManager pm = context.getPackageManager();
pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
return convertView;
And when onReceive, the program will execute the insert and update SQL statement if it matches the conditions:
ReminderAlarm class
public void onReceive(Context context, Intent intent) {
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
String recurID = intent.getStringExtra("RecurID");
String recurStartDate = intent.getStringExtra("RecurStartDate");
String date = intent.getStringExtra("Date");
String type = intent.getStringExtra("Type");
String amount = intent.getStringExtra("Amount");
String nextPaymentDate = intent.getStringExtra("NextPaymentDate");
String currentDate = "Next Payment On: "
+ dateFormat.format(new Date());
// If dates match then execute the SQL statements
if (currentDate.equals(nextPaymentDate)) {
DatabaseAdapter mDbHelper = new DatabaseAdapter(
context.getApplicationContext());
mDbHelper.createDatabase();
mDbHelper.open();
TransactionRecModel trm = new TransactionRecModel();
CategoryController cc = new CategoryController(mDbHelper.open());
trm.setDate(date);
trm.setTransDescription(description);
trm.setType(type);
trm.setAmount(Float.parseFloat(amount));
// Get the categoryID based on categoryName
String catID = cc.getCatIDByName(categoryName);
trm.setCategory(catID);
// Check if the recurring record exists before insert new
// transaction record
RecurringController rc1 = new RecurringController(mDbHelper.open());
boolean recurExist = rc1.checkRecurExist(recurStartDate,
description, catID);
if (recurExist == true) {
TransactionRecController trc = new TransactionRecController(
mDbHelper.open());
// Check if the transaction record exists to prevent duplication
boolean moveNext = trc.checkTransExist(trm);
if (moveNext == false) {
if (trc.addTransactionRec(trm)) {
// Update recurring start date after insertion of
// transaction
RecurringModel rm = new RecurringModel();
rm.setRecurringID(recurID);
rm.setRecurringStartDate(date);
RecurringController rc = new RecurringController(
mDbHelper.open());
if (rc.updateRecurringDate(rm)) {
mDbHelper.close();
}
}
}
}
}
In my manifest file:
<receiver android:name="ReminderAlarm"></receiver>
<receiver
android:name="BootReceiver"
android:enabled="false" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" >
</action>
</intent-filter>
</receiver>
Boot Receiver class:
public void onReceive(Context context, Intent i) {
scheduleAlarms(context);
}
static void scheduleAlarms(Context context) {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 1);
AlarmManager mgr = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
Intent notificationIntent = new Intent(context, ReminderAlarm.class);
PendingIntent pi = PendingIntent.getService(context, 0,
notificationIntent, 0);
mgr.setInexactRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pi);
}
However, the alarm manager does not execute when it get passed 12am everyday. The insert and update SQL statement will only be execute when I run the applications.
Any ideas? Thanks in advance.
Problem is in setting time for alarm to execute. replace the top three lines with this code
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 1);
Change your calendar value to Calendar calendar = Calendar.getInstance();
calendar .set(Calendar.HOUR_OF_DAY, 0);
calendar .set(Calendar.MINUTE, 0);
calendar .set(Calendar.SECOND, 0); and set alarm.
I am having some problem with Alarm Manager in Android.
Inside the onCreate method:
notificationCount = notificationCount + 1;
AlarmManager mgr = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
Intent notificationIntent = new Intent(context,
ReminderAlarm.class);
notificationIntent.putExtra("NotifyCount", notificationCount);
PendingIntent pi = PendingIntent.getBroadcast(context,
notificationCount, notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
mgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
60000+System.currentTimeMillis(), pi);
ComponentName receiver = new ComponentName(context, BootReceiver.class);
PackageManager pm = context.getPackageManager();
pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
Reminder Alarm class:
public class ReminderAlarm extends BroadcastReceiver {
private NotificationManager mNotificationManager;
private Notification notification;
#Override
public void onReceive(Context context, Intent intent) {
mNotificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent
.getActivity(context, 0, new Intent(), 0);
notification = new Notification(
R.drawable.ic_launcher, "Notification",
System.currentTimeMillis());
notification
.setLatestEventInfo(context, "AlarmActivated", "Hello", contentIntent);
mNotificationManager.notify(
Integer.parseInt(intent.getExtras()
.get("NotifyCount").toString()),
notification); }}
In the boot receiver class:
public void onReceive(Context context, Intent i) {
scheduleAlarms(context);
}
static void scheduleAlarms(Context context) {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 1);
AlarmManager mgr = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
Intent notificationIntent = new Intent(context, ReminderAlarm.class);
PendingIntent pi = PendingIntent.getService(context, 0,
notificationIntent, 0);
mgr.setInexactRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(),
60000+System.currentTimeMillis(),
pi);
}
And in my manifest file:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver android:name=".ReminderAlarm" >
</receiver>
<receiver
android:name=".BootReceiver"
android:enabled="false" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" >
</action>
</intent-filter>
</receiver>
I am trying to set the alarm to repeat every minute and prompt user notification. But it works in this way: when I launch the apps, it sets the alarm for once. After it prompt notification for once, then it stopped the repeating for every minute.
I wonder which part of my code went wrong. Thanks in advance.
EDIT
So basically I want the alarm manager to repeat everyday at 12am. Here is the codes:
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 0 );
calendar.set(Calendar.MINUTE, 1);
notificationCount = notificationCount + 1;
AlarmManager mgr = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
Intent notificationIntent = new Intent(context,
ReminderAlarm.class);
PendingIntent pi = PendingIntent.getBroadcast(context,
notificationCount, notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
mgr.setRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pi);
ComponentName receiver = new ComponentName(context, BootReceiver.class);
PackageManager pm = context.getPackageManager();
pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
So by setting the repeating as such, will it still run after 44 years as previous one? or it will repeat everyday at 12am?
The third parameter in the setInexactRepeating() method is the interval in milliseconds. You're setting that as the number of milliseconds that have elapsed since January 1, 1970 00:00:00.0 UTC plus 60000, making the next scheduled event about 44 years in the future. Change your code as follows:
mgr.setInexactRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), 60000, pi);