I am trying to set periodically alarm every 30 minutes, but i dont think it is working. I use below code:
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(MyActivity.this, Alarm.class);
PendingIntent alarmIntent = PendingIntent.getBroadcast(MyActivity.this, 0, intent, 0);
long selectedTimeMiliseconds = (long) (TimeUnit.MINUTES.toMillis(30));
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), selectedTimeMiliseconds, alarmIntent);
How to set the second parameter in method setInexactRepeating? Is it better to use setRepeating method?
Inexact is a save battery method, but it is... inexact.
AlarmManager have a problem, if the device is in sleep mode, the intent not start. You should use WakefulBroadCast.
https://developer.android.com/reference/android/support/v4/content/WakefulBroadcastReceiver.html
Try this..
alarmManager.setRepeating(AlarmManager.RTC, calendar.getTimeInMillis(), selectedTimeMiliseconds, alarmIntent);
Related
private void startAlarm() {
AlarmManager alarmMgr;
PendingIntent alarmIntent;
alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(MainActivity.this, AlarmReceiver.class);
alarmIntent = PendingIntent.getBroadcast(MainActivity.this, 0, intent, 0);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.MINUTE, 52);
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
1000 * 60 * 20, alarmIntent);
}
startAlarm() method is in the MainActivity class.
The following code should display a toast (declared in onReceive() method of the AlarmReceiver class) at 23:52 and every 20 minutes thereafter
AlarmReceiver extends BroadcastReceiver.
The code compiles without errors but for some reason it is not displaying the toast.
NOTE : The code worked fine with ELAPSED_REALTIME_WAKEUP. I am having problem with RTC_WAKEUP only. So everything else has to be correct. There is something wrong inside the startAlarm() method only.
I have tried your code with some modification as below .
private void startAlarm() {
AlarmManager alarmMgr;
PendingIntent alarmIntent;
alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(MainActivity.this, AlarmReceiver.class);
alarmIntent = PendingIntent.getBroadcast(MainActivity.this, 0, intent, 0);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 2);
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
1000 * 20, alarmIntent);
}
This code works fine with AlarmManager.RTC_WAKEUP. If it does not work for you, please reboot your device after installing it. After reboot completed, start your app. Then it should fire the alarm and every 20 sec interval the alarm fires repeatedly.
You can also check the difference between RTC_WAKEUP and ELAPSED_REALTIME_WAKEUP. From android docs, elapsedRealtime means, Returns milliseconds since boot, including time spent in sleep and RTC means, time in System.currentTimeMillis() (wall clock time in UTC).
Looks like You need to set an action to the intent:
Intent intent = new Intent(MainActivity.this, AlarmReceiver.class);
intent.setAction("YourPackageName.YourAction");
alarmIntent = PendingIntent.getBroadcast(MainActivity.this, 0, intent, 0);
And in the manifest:
<receiver android:name=".AlarmReciever">
<intent-filter>
<action android:name="YourPackageName.YourAction" />
</intent-filter>
</receiver>
This should fix the issue. You must have a unique action in the intent for use it with broadcast receiver.
In my app, I need to start a service at 2:00pm daily. Right now I wrote the code to trigger the alarm once, this code is ran every time I open the app:
AlarmManager alarmMgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, DownloadReceiver.class);
PendingIntent pIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmMgr.cancel(pIntent);
Calendar cal= Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
cal.set(Calendar.HOUR_OF_DAY,refreshhour);
cal.set(Calendar.MINUTE,refreshmin);
cal.set(Calendar.SECOND, 0);
// if the scheduler date is passed, move scheduler time to tomorrow
if (System.currentTimeMillis() > cal.getTimeInMillis()) {
cal.add(Calendar.DAY_OF_YEAR, 1);
}
if(android.os.Build.VERSION.SDK_INT>=23) {
alarmMgr.setAndAllowWhileIdle(AlarmManager.RTC_WAKEUP,
cal.getTimeInMillis(), pIntent);
}
else{
alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, pIntent);
}
Q1. I used setAndAllowWhileIdle() for sdk above 23 in case the device is in Doze mode. I cannot find any option in this function that I can set the alarm to repeat every day.
Q2. I also have questions about setInexactRepeating() , normally it is set to repeat every day by setting the parameter INTERVAL_DAY , but in the docs, it says
As of API 19, all repeating alarms will be inexact and subject to
batching with other alarms regardless of their stated repeat interval.
Does this mean INTERVAL_DAY does not work anymore, so how can I set the alarm daily without rerunning this function and reset alarmManager?
Try below code will solve your problem-
AlarmManager alarmMgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
boolean flag = (PendingIntent.getBroadcast(this, 0,
new Intent("totime.action.string"),
PendingIntent.FLAG_NO_CREATE) != null);
if(!flag)
{
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 14);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
Intent intent = new Intent("totime.action.string");
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) Data_Graph.this.getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),(24*60*60*1000), pendingIntent);
}
In my app, I have an AlarmManager set up that runs every 5 mins, triggering a service. However I find that in Lollipop phones, it works only after I reboot the phone. If I do not reboot the phone, the AlarmManager never runs. Please let me know if you might be able to help me out with some hints. Appreciate your help.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 120);
Intent intentAlarm = new Intent(appContext, CheckConnReceiver.class);
PendingIntent pendingIntent
= PendingIntent.getBroadcast(appContext, 0, intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT);
// create the object
AlarmManager alarmManager = (AlarmManager) appContext.getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), 300000, pendingIntent);
I need to prompt alarm according to the time values in DB. I came through lot of examples but confusing me. Could any body help to set a repeating alarm for given time like 2011-07-03 02:00:00:000 . and it should repeat for 5mints interval.
I suggest you create a service. The service can read the database and set the alarm using AlarmManager class. You can use the AlarmManager's set() or setRepeating() methods based on your use. http://developer.android.com/reference/android/app/AlarmManager.html#set%28int,%20long,%20android.app.PendingIntent%29
here is a sample of AlarmManager usage.
long triggerAtTime = SystemClock.elapsedRealtime() + triggerAfterTime;
AlarmManager mgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, YourAlarmReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
mgr.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, triggerAtTime, pi);
YourAlarmReceiver class (usually) can be a BroadcastReceiver where your logic goes on what happens when the alarm is triggered.
There is no public API for the Calendar application, though since it is a native UI for a Google Calendar, you could push an event over to the Google Calendar via its GData API.
You can use AlarmManager class
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 10);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
I want to set phones alarm in phone.
Can anybody say how to do that?
I got the code from net, and try but the alram not set to phone.
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
Intent activate = new Intent(this, Alaram.class);
AlarmManager alarams ;
PendingIntent alarmIntent = PendingIntent.getBroadcast(this, 0, activate, 0);
alarams = (AlarmManager) getSystemService(ALARM_SERVICE);
alarams.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), alarmIntent);
In this code we are provide intent as Alaram.class.
Then what should be written in this Alram.class so that alarm will set to the mobile.
I think you misunderstood what the AlarmManager is good for. It starts your code on a given Point, it doesn't set the phones ring tone.
If you want to set the Phones Alarm sound, this might be helpful (from the standard Android Music Player):
// Set the system setting to make this the current ringtone
MusicUtils.setRingtone(this, mService.getAudioId());
You are setting alarm time as current time in millisecond it will be exceed when the alarmmanager wake up....So,here i modified your code.check this it will work.That alaram will start after 5 seconds...i mean your Alaram activity will start within 5 seconds.
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
Intent activate = new Intent(this, Alaram.class);
AlarmManager alarams ;
PendingIntent alarmIntent = PendingIntent.getBroadcast(this, 0, activate, 0);
alarams = (AlarmManager) getSystemService(ALARM_SERVICE);
alarams.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis()+5000, alarmIntent);