How to set Alarm on android Oreo or Above? - android

I have implemented AlaramManager with background service, It works fine on devices below Oreo. But doesn't work on android Oreo and above as Google stopped background services from working on Oreo and Above.
Is there any source code or any kind resource which will help me to set alarms on android Oreo or above devices?
After searching for a while I found out about JobsIntentService, but couldn't found enough information about it. And I don't know if it is the way to go. Basically, I want to show notifications on particular dates and times.
Thanks in Advance.

Please try bellow method it will work for you.
/**
* Method for start Alarm on defined minute
* #param minutes Minute when you want to start after current time
* #param context
*/
public static void startAlarm(Context context, int minutes) {
Logger.print("AlarmReceiver startAlarm called");
Intent alarmIntent = new Intent(context, WakefulBroadcastReceiverService.class);
alarmIntent.setAction("testAPP");
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 123451, alarmIntent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
manager.cancel(pendingIntent);
long alarmPeriodicTime = System.currentTimeMillis() + Utils.getTimeInMilliSec(Constant.TimeType.MINUTE, minutes);
if (Build.VERSION.SDK_INT >= 23) {
manager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, alarmPeriodicTime, pendingIntent);
} else if (Build.VERSION.SDK_INT >= 19) {
manager.setExact(AlarmManager.RTC_WAKEUP, alarmPeriodicTime, pendingIntent);
} else {
manager.set(AlarmManager.RTC_WAKEUP, alarmPeriodicTime, pendingIntent);
}
}

Related

How to Make Repeating Alarm works even if device is in Doze mode

I've searched around but did not found the solution. My application is required to show daily notifications on different times decided by user but alarmmanager is not working properly, If I set time for next few minute it will show notification but most of the time when I'm not using device it do not works.
this is what i've been trying to set repeating alarm.
Intent intent = new Intent(Reminder.this, AlarmReceiverDaily.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(Reminder.this, 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) Reminder.this.getSystemService(ALARM_SERVICE);
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), 24 * 60 * 60 * 1000, pendingIntent);
How I can set repeating alarm even if my device is in Doze mode from the following code?
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
alarmManager.setAlarmClock(new AlarmManager.AlarmClockInfo(targetCal.getTimeInMillis(),pendingIntent),pendingIntent);
else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
alarmManager.setExact(AlarmManager.RTC, targetCal.getTimeInMillis(), pendingIntent);
else
alarmManager.set(AlarmManager.RTC, targetCal.getTimeInMillis(), pendingIntent);
If I can not use setAlarmClock() with repeating alarm then what solution you prefer to make repeating alarm and it must work.
You have to use:
if( Build.VERSION.SDK_INT >= 23 ){
alarmManager.setExactAndAllowWhileIdle( ... );
}

How to notify user with alarm when app is not running?

I have an app where I want to set alarm on specific date and time . Let say 5:00pm of 16 march. I want this alarm to be schedule as non repeating alarm.
Now the alarm is setting and working. but it is only working when App is working or in background. but when app is force closed it is not working.
here is what I am doing:
public void startAlert() {
EditText text = (EditText) findViewById(R.id.time);
int i = Integer.parseInt(text.getText().toString());
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();
}
Well this code is just for demonstration. The problem is when I have cleared my app from the app task manager the alaram does not alaram.
I know there are some problems, as android os unregister scheduled alarms , also there could be problem on reboot.
For the time Now I really do not care about about the reboot, but alarm should work when app is forced to close. If it is not applicable in android then how other apps are doing this ???
Please share your views about it ? and tell me anyother solution to how to notify my user at specific time of a day even my app is not there even in background ?? please help
for setting Alarm .please use below code
if (SDK_INT < Build.VERSION_CODES.KITKAT) {
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()
+ (i * 1000), pendingIntent);
}
else if (Build.VERSION_CODES.KITKAT <= SDK_INT && SDK_INT < Build.VERSION_CODES.M) {
alarmManager.setExact(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()
+ (i * 1000), pendingIntent);
}
else if (SDK_INT >= Build.VERSION_CODES.M) {
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()
+ (i * 1000), pendingIntent);
}
From AlarmManager
AlarmManager provides access to the system alarm services. These allow you to schedule your application to be run at some point in the future. When an alarm goes off, the Intent that had been registered for it is broadcast by the system, automatically starting the target application if it is not already running. Registered alarms are retained while the device is asleep (and can optionally wake the device up if they go off during that time), but will be cleared if it is turned off and rebooted

Android Alarm Manager API

I had been tasked with developing an alarm app. Something simple which lets users set an alarm at a specific time and select a music file that should play when the alarm goes off. The app works great on phone like Samsung, Motorola, and Nexus but on Xiomi, Oppo, Vivo phones the alarm does not go off at the correct times and sometimes it does not fire at all. I have used Android's AlarmManager APIs. I am wondering if there is something else I need to do to get the alarm to work as expected on devices like xiomi, oppo, and vivo. Most of the solutions on StackOverflow have not worked out for me. If there are devs here in this subreddit who have worked with AlarmManager APIs before or worked on Apps with Alarm features, id really appreciates some more insight into AlarmManager APIs and why they don't work as expected across all phones and if there are any alternatives I should be using.
Bellow is the method which will help you, Which I tested with the different-different device it's working.
/**
* Method for start Alarm on Defined time
*
* #param context
*/
public static void startAlarm(Context context,int minutes) {
Logger.print("AlarmReceiver startAlarm called");
Intent alarmIntent = new Intent(context, WakefulBrodcastReceiverService.class);
alarmIntent.setAction("testAPP");
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 123451, alarmIntent, 0);
AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
manager.cancel(pendingIntent);
long alarmPeriodicTime = System.currentTimeMillis() + Utils.getTimeInMilliSec(Constant.TimeType.MINUTE, minutes);
if (Build.VERSION.SDK_INT >= 23) {
manager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, alarmPeriodicTime, pendingIntent);
} else if (Build.VERSION.SDK_INT >= 19) {
manager.setExact(AlarmManager.RTC_WAKEUP, alarmPeriodicTime, pendingIntent);
} else {
manager.set(AlarmManager.RTC_WAKEUP, alarmPeriodicTime, pendingIntent);
}
}

Android Studio Alarm Service Not Starting Automatically

I am trying to develop a simple alarm clock app in Android Studio, but I am having trouble figuring out how to use the AlarmManager and BroadcastReceiver properly.
In my MainActivity I have instantiated everything I need for the AlarmManager (Intent, PendingIntent, and I am using a Calendar to hold the time received from a TimePicker). I have this line in a button's onClick event
alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);
The receiver is called AlarmReceiver and in its onReceive() I have, for now,
Toast.makeText(context, "I'm running", Toast.LENGTH_SHORT).show();
Log.e("Receiver", "Receiver entered");
For testing purposes, I set the alarm time to be one minute from the current time whenever I debug the app. After setting the time, I press the button which sets the AlarmManager. The Toast message and Log message don't show up on their own once the time becomes the alarm time. They only show up if I press the button when the time is within the minute that is the alarm time.
For example if alarm time is 3:00, and current time is 2:59 and I have pressed the button to set the AlarmManager. When it becomes 3:00, the messages don't show. They will only show if I press the button within the minute of 3:00.
Is this the normal behavior? If so how do I set it up so that the messages show automatically once everything is set so that I can extend it to a ringtone later?
I have ensured that the time values are right and that the receiver is specified in the manifest
try to something like this:
private static void setUpAlarm(final Context context, final Intent intent, final int timeInterval){
final AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
final PendingIntent pi = PendingIntent.getBroadcast(context, timeInterval, intent, 0);
am.cancel(pi);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
{
final AlarmManager.AlarmClockInfo alarmClockInfo = new AlarmManager.AlarmClockInfo(System.currentTimeMillis() + timeInterval, pi);
am.setAlarmClock(alarmClockInfo, pi);
}
else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
am.setExact(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + timeInterval, pi);
else
am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + timeInterval, pi);
}
I'm really recommend to use this code:
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion < android.os.Build.VERSION_CODES.KITKAT){
am.set(AlarmManager.RTC_WAKEUP, nexttime, pi);
} else if (currentapiVersion < android.os.Build.VERSION_CODES.M) {
am.setExact(AlarmManager.RTC_WAKEUP, nexttime, pi);
} else {
am.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, nexttime, pi);
}

Android AlarmManager defaulting to 5 minutes when phone screen is off

So I am having an issue with AlarmManager. I am trying to run some code every 2 minutes, which works okay while the phone is awake, but not when the device goes to sleep - during sleep, the intervals are perfectly 5 minutes apart.
Since my desired interval is 2 minutes, this is roughly 250% off my target interval, which for my specific application is not acceptable.
I am aware of the changes in API 19, and have followed suggestions to re-schedule the alarm using setExact() within my BroadcastReceiver. Code is below:
The code used to trigger BroadcastReceiver:
Intent intent = new Intent(this, AlarmReceiver.class);
final PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
mAlarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
mAlarmManager.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 3000, pendingIntent);
And this code is in my BroadcastReceiver which re-schedules the alarm:
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "AlarmService Triggered.", Toast.LENGTH_SHORT).show();
Log.d(this.getClass().getSimpleName(), "Service triggered");
Intent newIntent = new Intent(context, this.getClass());
mPendingIntent = PendingIntent.getBroadcast(context, 0, newIntent, 0);
AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
alarmManager.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 120000, mPendingIntent);
}
Does anyone have suggestions as to how I could fix this? It is very frustrating that AlarmManager is totally ignoring my wish to have an alarm fire at an exact time. Is there any alternative that will allow me to schedule code at 2 minute intervals like I want?
DEVICE: Samsung Galaxy S6, OS 5.1.1
As #Francesc suggested, it ended up being the Samsung device. Tried it on another phone, now it works flawlessly.
Let this be a lesson to you - don't buy Samsung, they do weird stuff, lol.
Replace ELAPSED_REALTIME_WAKEUP with RTC_WAKEUP.
This is how I schedule alarms:
mTargetTime = System.currentTimeMillis() + timeout * 1000L;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
alarmManager.setAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, mTargetTime, mPendingIntent);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
alarmManager.setExact(AlarmManager.RTC_WAKEUP, mTargetTime, mPendingIntent);
} else {
alarmManager.set(AlarmManager.RTC_WAKEUP, mTargetTime, mPendingIntent);
}

Categories

Resources