I am trying to play a ringtone at exactly at 7 PM everyday but it is playing ringtone immediately after its pending intent is registering broadcast.
I called the service in the foreground on a button click and created pending intent there in onStartCommand:
#Override
public int onStartCommand(Intent intent, int flags, int startId)
{
startForeground(FOREGROUND_ID,
buildForegroundNotification("DummyApp"));
c = Calendar.getInstance();
AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
int interval = 1000 * 60 * 60*24;
c.setTimeInMillis(System.currentTimeMillis());
c.set(Calendar.HOUR, 19);
c.set(Calendar.MINUTE,00);
manager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(),
interval, pendingIntent);
Intent alarmIntent = new Intent(AlarmService.this, DataProcessor.class);
pendingIntent = PendingIntent.getBroadcast(AlarmService.this, 0,
alarmIntent, 0);
return START_STICKY;
}
Now I am playing a ringtone on receiving this broadcast in DataProcessor class the on Receive method of Data Processor class:
#Override
public void onReceive(Context ctx,Intent intent) {
playRIng(ctx);
}
But when I run this code,click the button,service is created but alarm is fired immediately after the AlarmService is called and ringtone is played also.How it is possible because I am giving the exact 7 O clock time when registering broadcast.?
Googled a lot but found the same code only and nothing else.Every code is able to play the ringtone on the time but it also plays the ringtone immediately after the broadcast is registered.
Why you don't use a condition?:
#Override
public void onReceive(Context ctx,Intent intent) {
Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR);
if(hour==19)
{
playRIng(ctx);
}
}
If you ran that code at 9pm you would be telling the AlarmManager that the intent should have been run for the first time 2 hours ago.
You need to check if the Calendar time is behind the current time.
If it is you need to add a day to the calendar so that it first triggers tomorrow at 7pm.
Something like this might work:
c = Calendar.getInstance();
AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
int interval = 1000 * 60 * 60 * 24;
// Not needed
// c.setTimeInMillis(System.currentTimeMillis());
c.set(Calendar.HOUR, 19);
c.set(Calendar.MINUTE, 00);
// ** Add this **
// Check if the Calendar 7pm is in the past
if (c.getTimeInMillis() < System.currentTimeMillis())
c.add(Calendar.DAY_OF_YEAR, 1); // It is so tell it to run tomorrow instead
manager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), interval, pendingIntent);
Intent alarmIntent = new Intent(AlarmService.this, DataProcessor.class);
pendingIntent = PendingIntent.getBroadcast(AlarmService.this, 0, alarmIntent, 0);
You can try the example at developers website understanding the samplecode available there.May be there is some programming mistake in your code but the sample code available there is working exactly you want.
Scheduling Repeating Alarms
Best way, Play with hour & mintue.
Note: Please check your time formate. In my case it's 24 hr formate
Example:
Calendar c = Calendar.getInstance();
//for 12 hr formate user int hour = c.get(Calendar.HOUR);
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
//Log.e("time",""+hour+":"+minute);
if (hour == 10 && minute == 0 ) {
}
Related
I have two alarm which fires at an interval of 3 min. What will I store in the Room database so that I can know which alarm is firing? I am using TIME_ONE and TIME_TWO to pass a value and detecting in the receiver end. But I think it is bad practice for large number of alarms. It would be better it I can match the id of the alarm that will fire with the stored id in the Room database. Or if I store store the requestcode in the db then how will I get the request code in the onReceive method. I have also tried alarmIntent.putExtra(TIME_ONE, 2); and alarmIntent.putExtra(TIME_ONE, 1); I am not sure if one name can hold two values and work fine. What will be the id of the alarm so that I can get the exact alarm in the onReceive method as it fires? If I pass the requestcode using putExtra should I use one tag (TIME_ONE) or multiple tags(TIME_ONE, TIME_TWO) to detect alarms? I don't want to use if else to check which one throwing 1 so that I can handle. How can I use intent of the onReceive method so that I can know directly which alarm is firing? You can imagine the scenario as two alarms set time to alarm and detect for which alarm the onReceive method is called.
private void setAlarm() {
Intent alarmIntent = new Intent(this, MyAlarmReceiver.class);
alarmIntent.putExtra(TIME_ONE, 1);
pendingIntent = PendingIntent.getBroadcast(this, 1, alarmIntent, 0);
manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
int interval = 1000 * 60 * 3; // 3 min
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 22);
calendar.set(Calendar.MINUTE, 1);
manager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), interval, pendingIntent);// repeat every two min interval
}
private void setSecAlarm() {
Intent alarmIntent = new Intent(this, MyAlarmReceiver.class);
alarmIntent.putExtra(TIME_TWO, 1);
pendingIntent = PendingIntent.getBroadcast(this, 2, alarmIntent, 0);
manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
int interval = 1000 * 60 * 3; // 3 min
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 22);
calendar.set(Calendar.MINUTE, 3);
manager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), interval, pendingIntent);// repeat every two min interval
}
The onReceive method:
Calendar instance = Calendar.getInstance();
#Override
public void onReceive(Context context, Intent intent) {
int hour = instance.get(Calendar.HOUR);
int minute = instance.get(Calendar.MINUTE);
int second = instance.get(Calendar.SECOND);
int which_one = intent.getIntExtra(TIME_ONE,0);
int which_two = intent.getIntExtra(TIME_TWO,0);
if(which_one == 1 && which_two == 0){
Log.d(MY_TAG, hour+":"+minute+":"+second+ " First alarm called");
}else if(which_one == 0 && which_two == 1){
Log.d(MY_TAG, hour+":"+minute+":"+second+" Second alarm called");
}else{
Log.d(MY_TAG, hour+":"+minute+":"+second+ " none of them");
}
}
Late answer, hope you've solved this already of course .
I 'd just pass an id of the database row in the alarm intent.
The Insert Dao will return an Id, use it as the alarm identifier, and in onreceive you directly get the relevant row.
I'm currently trying to make an alarm app that will mute all audio volume at the user-defined day and time. However, the alarm seems not fired at the correct time. I found that the alarm is always fired earlier.
This is my set alarm code:
public static void setAlarm(Context context, String time, int day, int reqCode) {
String[] timeSplit = time.split(":");
Calendar calendar = Calendar.getInstance();
int hour = Integer.parseInt(timeSplit[0]);
int minute = Integer.parseInt(timeSplit[1]);
int days = (day-calendar.get(Calendar.DAY_OF_WEEK) + calendar.get(Calendar.DAY_OF_MONTH));
calendar.set(
calendar.get(Calendar.YEAR),
calendar.get(Calendar.MONTH),
days,
hour,
minute,
0
);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, RuleAlarm.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, reqCode, intent, 0);
// Set the alarm to be fired every week on the selected day
alarmManager.setRepeating(AlarmManager.RTC, calendar.getTimeInMillis(), 7 * 24 * 60 * 60 * 1000, pendingIntent);
Toast.makeText(context, "Alarm is set", Toast.LENGTH_SHORT).show();
}
I have tried to change setRepeating() to setInexactRepeating() but nothing changed.
AlarmManger is inexact to save battery by combining wakeup calls.
https://developer.android.com/reference/android/app/AlarmManager.html
Try this
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 7 * 24 * 60 * 60 * 1000, pendingIntent);
The problem is that if you are creating PendingIntent for an alarm for past time it will be fired immediately.If you would like to prevent that happening, then simply do not set alarms with a past trigger time . Use BroadcastReceiver for receiving alarm.
Update :
Check we aren't setting it in the past which would trigger it to fire instantly
if(calendar.getTimeInMillis() < System.currentTimeMillis()) {
calendar.add(Calendar.DAY_OF_YEAR, 7);
}
I finally find out the problem. It lies on the day params. What I pass to day params is my Spinner selected position which starts from zero while the Calendar days starts from one (SUNDAY = 1, MONDAY = 2, so on). So, the alarm will always be set in the past days. I plus one to the day params and it's worked.
This is my piece of code where the startAlarm() function got called.
String ruleStartTime = rule_startTime.getText().toString();
// Spinner index starts from zero,
// to match with the Calendar day indexing, we need to plus it one
int dayIdx = day.getSelectedItemPosition()+1;
Alarm.setAlarm(getApplicationContext(), ruleStartTime,
dayIdx, 0);
I want to do a specific task each week .
So I used an Alarm Manager
when user click button alarmManager running
blow code Works properly:
public void set_alarm(int reapte)
{
Context context=getBaseContext();
Calendar calendar=Calendar.getInstance();
// calendar.set(Calendar.HOUR_OF_DAY, 20); // For 1 PM or 2 PM
//calendar.set(Calendar.MINUTE, 41);
Intent intent= new Intent(context, MyService.class);
intent.putExtra("size", reapte);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pi = PendingIntent.getService(context, 0,
intent ,PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY*7, pi);
}
The problem that I have.
When the user clicks. Alarm Manager runs the same moment.
And next week will run.
I want to first run the alarm from next week
.sorry My language is not good
Try this
you can set the day from which alarm will begin.
calendar.set(Calendar.DAY_OF_WEEK, 1);
where sunday=1 and so on to sat=7
and set the time 2 min before from current time when alarm was set,so it will same day of next week
I finally find the answer
WEEK_OF_MONTH values is between 1 and 4.
so
I use below code:
int week=calendar.get(Calendar.WEEK_OF_MONTH);
int day=calendar.get(Calendar.DAY_OF_WEEK);
int hour=calendar.get(Calendar.WEEK_OF_YEAR);
int minute= calendar.get(Calendar.MINUTE);
calendar.set(Calendar.DAY_OF_WEEK, day);
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE,minute);
int next_week=week+1;
if(next_week>4)
next_week=1;
calendar.set(Calendar.WEEK_OF_MONTH, next_week);
//other code
I have this alarm manager which I want to fire at 6 AM everyday. I call the class notification, when the app is first launched and hence, it sets an repeating alarm for 6 AM every day(repeatedly).
The alarm manager works fine when the user installs(and runs the app first time) from 6 to 9 AM, however if the user runs the app for the first time after 9 AM, I'm supposed to add a day and fire the alarm the next day at 6AM. On doing so, (changing the date to the next day), the alarm gets fired at a random time, around 10 PM (That's a huge difference). Here's my code"
public notification(Context context) {
this.context=context;
Intent appstart=new Intent(context,appstartreceiver.class);
PendingIntent pi=PendingIntent.getBroadcast(context,0,appstart,PendingIntent.FLAG_UPDATE_CURRENT); // For appstart in the morning
AlarmManager alarmManager = (AlarmManager)context.getSystemService(context.ALARM_SERVICE);
Calendar currentCal=Calendar.getInstance();
Calendar startapp = Calendar.getInstance();// FOr app start in the morning
startapp.set(Calendar.HOUR_OF_DAY,6);
startapp.set(Calendar.MINUTE, 0);
startapp.set(Calendar.SECOND, 0);
long currentTime = currentCal.getTimeInMillis();
int diff=(int)(currentTime-startapp.getTimeInMillis())/(1000*60*60);
if (diff<3)
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,startapp.getTimeInMillis(),1000*24*60*60,pi);
else {
startapp.add(Calendar.HOUR_OF_DAY, 24);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, startapp.getTimeInMillis(), 1000 * 24 * 60 * 60, pi);
}
}
Replace your code with below code...this will fire everyday at 6 am. It may helps you.
public void notification(Context context) {
Intent intent = new Intent(this, appstartreceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 99, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 6);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
long startUpTime = calendar.getTimeInMillis();
// To avoid firing the alarm if the time is passed while setting
if (System.currentTimeMillis() > startUpTime) {
startUpTime = startUpTime + 24 * 60 * 60 * 1000;
}
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, startUpTime, 24 * 60 * 60 * 1000, pendingIntent);
}
You could try adding a day
if (calendar.before(Calendar.getInstance())) {
calendar.add(Calendar.DAY_OF_MONTH, 1);
}
....// your code
That way it will fire everyday at the set time.
Try commonwares's wakeful intent service,it may help.
public class DailyListener implements AlarmListener {
public void scheduleAlarms(AlarmManager mgr, PendingIntent pi, Context context) {
// every day at scheduled time
Calendar calendar = Calendar.getInstance();
// if it's after or equal 6 am schedule for next day
if (Calendar.getInstance().get(Calendar.HOUR_OF_DAY) >= 6) {
calendar.add(Calendar.DAY_OF_YEAR, 1);
}
calendar.set(Calendar.HOUR_OF_DAY, 6);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
mgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pi);
}
}
I know this is most common thread in SO but i didn't find any answer for my question . I want to trigger dynamic broadcast receiver at particular time. For this i am using below code
public void method1() {
br = new BroadcastReceiver() {
#Override
public void onReceive(Context c, Intent i) {
doTASK();
}
};
registerReceiver(br, new IntentFilter("com.XXXX.wakeup"));
pi = PendingIntent.getBroadcast(this, 0,
new Intent("com.xxxx.wakeup"), 0);
am = (AlarmManager) (this.getSystemService(Context.ALARM_SERVICE));
setAlarmManagerObject();
}
And setAlarmManagerObject() method is here
public void setAlarmManagerObject() {
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH);
int day = cal.get(Calendar.DAY_OF_MONTH);
cal.set(year, month, day
, com.XXXX.Constants.AlarmHour
, com.xxxx.Constants.AlarmMinute
, com.xxxx.AlarmSecond);
am.setRepeating(AlarmManager.ELAPSED_REALTIME, cal.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, pi);
}
My problem is that when i used AlarmManager.RTC_WAKEUP it wake up when device is off. I don't want this property in my app. So i used AlarmManager.ELAPSED_REALTIME, but with ELAPSED_REALTIME alarm manager is not triggering. I want that my alarm manager trigger exact at same time. If device is off or sleep condition then alarm manager will also off after device wake up.It should trigger at exact time. Any help will really appreciated.
You need to use something like this:-
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP (or AlarmManager.ELAPSED_REALTIME),
SystemClock.elapsedRealtime() + Constants.DELAY_IN_MILLIS,
pendingIntent);
You can calculate DELAY_IN_MILLIS which is time delay in milliseconds from current system time.
For repeating alarm you can have code something like this:-
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime() + Constants.DELAY_IN_MILLIS,
Constants.ONE_DAY_IN_MILLIS,
pendingIntent);