I am making an alarm app for waking up user at an exact time.
I am using JobIntentService (since Android O+ devices don't support background services) for showing a notification alarm like this:-
(AlarmManager -> BroadcastReceiver -> JobIntentService -> ShowNotification).
But now on Android O+ devices the JobIntentService gets delayed due to the doze mode and the notification alarm is not shown at the exact time.
Even when i use setExactAndAllowWhileIdle() the AlarmManager gets out of doze mode but JobIntentService still gets delayed.
I can't figure out any alternatives for my case/scenario other than the option below:-
Since the user will have to play a game to stop\snooze the alarm,
a persistent foreground service will keep playing the alarm until user opens the app(by tapping the foreground service). The alarm will be kept playing until the user plays the game. The foreground service (on-going alarm) will stop once the user is done playing the game.
Related
I have created an alarm app years ago, it vibrates in background (while app is closed), using Alarm and BroadcastReceiver.
But now in Android Q (v10), I can't start vibrate from background and I should run a foreground service. The new problem is if device is locked and screen is off, sometimes system decides to hold/freeze the foreground service until screen is turned on by user, then it will vibrate, maybe minutes after real alarm time!
Is there any trick to vibrate without running a foreground service?
I am developing an application, one of the functions of which is the alarm clock. I saw that some applications with such a function can display the time of the next alarm clock, as if it were a system alarm clock. What API can I do this for?
I tried to send the broadcast android.intent.action.ALARM_CHANGED, but this does not work on my device (and somehow it works for other applications).
I am building an Android Alarm Applicaion, and used following code for Main Calling Class:
AlarmManager inst_alarm= (AlarmManager) getSystemService(ALARM_SERVICE);
Intent intent =new Intent(MainActivity.this,Alarm.class);
pintent= PendingIntent.getBroadcast(MainActivity.this, 0, intent, 0);
inst_alarm.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),pintent);
//"cal" is reference of calendar class to get saved time in millisecond.
for Service Class:
public class Alarm extends WakefulBroadcastReceiver {
#Override
public void onReceive(Context c, Intent i) {
Uri uri_alarm= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
if(uri_alarm==null)
{ uri_alarm=RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
}
Ringtone ringtone_alarm=RingtoneManager.getRingtone(c, uri_alarm);
ringtone_alarm.play();
}
}
This code work fine but i have few quesions.
Question: 1) Is it possible to alarm bell on scheduled time if android device is switched off(device get power on automatically to ring bell on saved time) as in default application of android device does.
2) Suppose i saved alarm after 8 minutes exactly and i restart my device then alarm start ringing immediately after boot(Because i have used "bootcomplete" for receiver in manifest) but i want to play alarm after 8 minutes exactly not on reboot.(I have saved alarm in shared-preference but how to use it on reboot)
EDIT: Can I use "Power Manager" class or any other class to handle above mentioned situation ?
1) Is it possible...
No, not from complete power off. You're most likely referring to screen being off and the device in low power state. If the device is turned completely off then alarms cannot wake it. The default Android clock app just wakes it from sleep. If you'd like to wake up the screen and play sound, you'll have to create a Service which can be activated via your WakefulBroadcastReceiver and have it start the appropriate UI. If you do not, then the system can go right back to seep when your onReceive() is done.
2) ...want to play 8 minutes after...
What you're likely seeing is a previous expiration of your Alarm firing and you're waking up the device (not powering it on.) Alarms are not retained across power cycles or reboots. If you wish to have them stick around after a true reboot, then you'd have to manage that yourself. The PowerManager and AlarmManager do not provide such a facility.
For (1), if you're talking about complete shutdowns, it's unfortunately impossible. There is no way to do this without specialized hardware (and its corresponding software interface to your app).
For (2), you can do something clever. For example, persistently store the timestamp whenever the device restarts/turns off (you can register a BroadcastReceiver for ACTION_SHUTDOWN in addition to your BOOT_COMPLETE) when you have a pending alarm bell, and use that information on reboot/boot to resume (or stop, depending on the time difference and the alarm bell's "timer") or otherwise re-sync your pending alarm bell logic.
And for your PowerManager question, again if we're talking about complete shutdowns, unfortunately the answer is no.
PowerManager is used to mainly prevent the device from going into deep sleep. Remember, Android devices turn off their CPU's some time after they are locked, preventing your app from doing calculations/processes. This is essentially what the notion of "wakelocks" is about.
I followed this post http://it-ride.blogspot.com/2010/10/android-implementing-notification.html to implement a Notification Service. It uses AlarmManager and Wakelock to start a service. When you restart the device the alarm is set correctly or when you open the app the alarm is set, but if you force close the app the alarm is gone. I would like to reset the alarm after the app is forced stopped. How can I do this?
According to Google's Android engineers, an app that is forcefully stopped will not start again until the user invokes it.
Check out the following response found here.
I am developing a reminder application. I am generating notifications using notification manager class, when the timeline crosses.But if my cell phone is switched off ,I am unable to see these notifications. Not even when i switch it on again.
Even if i switch it off and switch on again, i think the pending intents are destroyed and no notification is generated.
How do i get it when the phone is switched on again ?
Have a look at the AlarmManager:
From http://developer.android.com/reference/android/app/AlarmManager.html:
This class 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.