If i sett HOUR=11 and MINUTE=00, it wont run at time 11:00 in my emulator, is something wrong here?:
Calendar cal = Calendar.getInstance();
PendingIntent pintent = PendingIntent.getService(this, 0, new Intent(this, MyService.class), 0);
cal.set(Calendar.MINUTE, MINUTE);
cal.set(Calendar.HOUR_OF_DAY, HOUR);
cal.add(Calendar.DAY_OF_YEAR,1);
AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 86400000, pintent);
running = true;
This alarm will be work 11:00am..
Calendar cal = Calendar.getInstance();
Intent intent = new Intent(this, MyService.class);
PendingIntent penintent = PendingIntent.getService(this, 0, intent, 0);
AlarmManager alarm = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
//Here I have set it to 11.00am
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 11);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
//set timer as a RTC Wakeup to alarm manager object
alarm.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY, penintent);
I suggest, try to use this solution.
Related
MainActivity.java
Intent intent = new Intent(this, AlarmReceiver24.class);
PendingIntent alarmIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
AlarmManager alarmMgr = (AlarmManager) getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY,1);
calendar.set(Calendar.MINUTE, 50);
calendar.set(Calendar.SECOND, 0);
String time= String.valueOf(calendar.getTime());
Log.i("Time:",time);
//repeat alarm every 24hours
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY, alarmIntent);
AlarmReceiver class
public class AlarmReceiver24 extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
reference.child("Total").setValue(0);
Toast.makeText(context, "Total Reset", Toast.LENGTH_SHORT).show();
}
}
Android Manifest
<receiver android:name=".AlarmReceiver24"/>
I want the code to run at midnight 12:00:00 once everyday but it keeps firing again and again even after using AlarmManager.INTERVAL_DAY. I don't know what I'm doing wrong.
Each time you opens MainActivity it creates a new PendingIntent
When you start MainActivity you should remove all previously scheduled alarms.
Also
calendar.set(Calendar.HOUR_OF_DAY, 1);
calendar.set(Calendar.MINUTE, 50);
calendar.set(Calendar.SECOND, 0);
is not midnight.
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
is midnight
The Alarm was getting set in past so added this condition and it was solved
if(calendar.before(Calendar.getInstance()))
calendar.add(Calendar.DATE,1);
I start to combine alarm manager and notification manager on android, and this is my code:
MainActivity.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setReminderAlarm();
}
public void setReminderAlarm() {
Intent intent = new Intent(MainActivity.this, ReminderReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, ReminderReceiver.NOTIF_ID, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 6);
calendar.set(Calendar.MINUTE, 59);
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 86400000L, pendingIntent);
}
ReminderReceiver.java
public static int NOTIF_ID = 101;
#Override
public void onReceive(Context context, Intent intent) {
showAlarmNotification(context);
}
public void showAlarmNotification(Context context) {
Intent intentClick = new Intent(context, MainActivity.class);
PendingIntent pendingClick = PendingIntent.getActivity(context, 0, intentClick, 0);
NotificationManager notificationManagerCompat = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.cinema)
.setContentTitle(title)
.setContentIntent(pendingClick)
.setContentText(message)
.setColor(ContextCompat.getColor(context, android.R.color.transparent))
.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000})
.setSound(alarmSound)
.setAutoCancel(true);
notificationManagerCompat.notify(NOTIF_ID, builder.build());
}
Every time I run the application, the notification always appears even though I have set the time. How do I prevent that?
Thank you.
The issue is you are setting the time to current day. So if you are opening your app after 6:00, your alarm manager will immediately fire.
You need to check whether the time is over 6:00 for the current day, if yes you need to change the date to the next day:
public void setReminderAlarm() {
Intent intent = new Intent(MainActivity.this, ReminderReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, ReminderReceiver.NOTIF_ID, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
int curHr = calendar.get(Calendar.HOUR_OF_DAY);
// Checking whether current hour is over 6
if (curHr >= 6)
{
// Since current hour is over 6, setting the date to the next day
calendar.add(Calendar.DATE, 1);
}
calendar.set(Calendar.HOUR_OF_DAY, 6);
calendar.set(Calendar.MINUTE, 59);
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 86400000L, pendingIntent);
}
I tried setting AlarmManager.setRepeating() state but not getting any success,
I tried setting Alarm yesterday at 10 AM and got alarm notification for that current day only, next day I didn't get any notification at 10 AM.
Below is my code please let me know what's going wrong in my code.
Intent intent = new Intent(this, AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(ReminderActivity.this,0,intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
long timeInMillis = System.currentTimeMillis();
Log.i(TAG, "timeInMillis: " + timeInMillis);
Logger.addRecordToLog(ReminderActivity.this,"hours "+hour +"minutes "+minute,"triggered "+new Date());
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, minute);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
Log.i(TAG, "hour: " + hour);
Log.i(TAG, "minute: " + minute);
Log.i(TAG, "" + calendar.getTimeInMillis());
Logger.addRecordToLog(ReminderActivity.this,"calendar Alaram Time"+calendar.getTimeInMillis(),"triggered "+new Date());
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
****************************BroadCast Reciver Class***************
public class AlarmReceiver extends BroadcastReceiver {
private final static String TAG = "AlarmReceiver";
#Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "onReceive()");
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
String current_date = dateFormat.format(date);
Logger.addRecordToLog(context,"AlarmReceiver","Received daily cup reminder "+current_date);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_daily_cup)
.setContentTitle(context.getString(R.string.daily_cup_reminder))
.setContentText(context.getString(R.string.daily_cup_message))
.setAutoCancel(true);
resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context,0,resultIntent,PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(pendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, mBuilder.build());
}
}
Try this
Calendar calendar = Calendar.getInstance();
if (Calendar.getInstance().get(Calendar.HOUR_OF_DAY) >= 10) {
calendar.add(Calendar.DAY_OF_YEAR, 1); // add, not set!
}
calendar.set(Calendar.HOUR_OF_DAY, 10);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setInexactRepeating(AlarmManager.RTC, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, pi);
Try to set proper triggerAtTime (in the future) - like
Calendar.getInstance().getTimeInMillis()+(24*60*60*1000)
I am able to send one broadcast per day, but I'm trying to send more than one per day. It seems like calling the same function over again is writing over the previous broadcast because I'm only receiving the last broadcast. here is the code
send(22,38);
send(22,39);
send(22,40);
public void send(int hour, int minute){
AlarmManager alarmMgr;
PendingIntent alarmIntent;
Context context = this;
alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmReceiver.class);
alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, minute);
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, alarmIntent);
}
I have a sound that is scheduled to play at 9 pm every day using the alarm manager class in android. How can I set a time for it to stop (for example, have it start at 2 pm and stop at 11 pm)?
void playAudio() {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 21);
AlarmManager alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent objIntent = new Intent(this, Sound.class);
PendingIntent alarmIntent = PendingIntent
.getService(getApplicationContext(), 0, objIntent,
PendingIntent.FLAG_ONE_SHOT);
alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, alarmIntent);
}