Android Studio Alarm Service Not Starting Automatically - android

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);
}

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 set Alarm on android Oreo or Above?

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);
}
}

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

alarm manager not working while updating interval time

after reading all the QA i didnt get any proper solution.
I have 2 problems
1. Alarm fires twice even if i register my receiver only in manifest.(not by code)
2. when i update interval time of alarm it gets fires randomly
here is my method for set alarm
public void AlarmCall(int min) {
Intent intent = new Intent(context, AlarmReceiver.class);
PendingIntent pintent = PendingIntent.getBroadcast(context,0 , intent, 0);
alarm = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
cancelAlarm(alarm,pintent);
if(Build.VERSION.SDK_INT<18) {
alarm.set(AlarmManager.RTC_WAKEUP, 1000 * 60 * min, pintent);
}
else if(Build.VERSION.SDK_INT>=19 && Build.VERSION.SDK_INT<=22)
{ alarm.setRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis(), 1000*60*min, pintent);
}
else if(Build.VERSION.SDK_INT>=23)
{ alarm.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP,1000*60*min,pintent);
}
}
method to cancel alarm :
public void cancelAlarm(AlarmManager alarm,PendingIntent p)
{
alarm.cancel(p);
Log.d("Alarm","Alarm Cancle");
}
in my project Application class i have to start alarm with 10 min time interval and it works fine , according to user input value i need to update time interval.
so i call this method with int min input value and cancel first alarm.
but in marshmallow it fires at every 5 seconds, and kitkat lollipop it fires randmoly.
even checked with setExact() method
I had the same issue, use setWindow solved the problem
long repeatInterval = 1000 * 60 * min;
long triggerTime = SystemClock.elapsedRealtime()
+ repeatInterval;
AlarmManager alarms = (AlarmManager) this.getSystemService(
Context.ALARM_SERVICE);
if (Build.VERSION.SDK_INT >= 19)
{
alarms.setWindow(AlarmManager.RTC_WAKEUP,
triggerTime,
repeatInterval,
pendingIntent);
}else{
alarms.setInexactRepeating(AlarmManager.RTC_WAKEUP,
triggerTime,
repeatInterval,
pendingIntent);}

Alarm Manager doesn't work on time

I have this method which should schedule alarms but when the time arrives it doesn't start the pendingintent ??
public void setAlarm(String name, long time) {
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent dialog = new Intent(this, SubActivity.class);
dialog.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pi = PendingIntent.getActivity(this, 0, dialog, 0);
if (Build.VERSION.SDK_INT >= 19) {
if (System.currentTimeMillis() < time) {
am.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, time, pi);
}else{
time+=(AlarmManager.INTERVAL_DAY*7);
am.setExact(AlarmManager.RTC_WAKEUP, time, pi);
}
} else {
am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, time, AlarmManager.INTERVAL_DAY * 7, pi);
}
}
The problem is you are using a PendingIntent for an Activity, which will not necessarily keep the device awake long enough for the Activity to get started. You'll have to use a PendingIntent for a BroadcastReceiver which leverages a wake lock to keep the device awake until your app code can run. WakefulBrodcastReceiver is a good choice, or you can roll your own as needed. See this article for an explanation and sample of how to use alarms to wake the device: http://po.st/7UpipA

Categories

Resources