I am new to android. I am making an alarm clock. Its working perfectly until the user clears the app from the RAM. On searching, I found that broadcast receivers don't work if the app is cleared from the RAM. So, what exactly should I do? Will sending the broadcast from a service help? Also if you have a link to a good tutorial to Services in android, pls do share. Also let me know if there is some other way to solve my problem.

- 332
- 4
- 17
-
Which phone are you testing on? – zeeali Apr 21 '16 at 10:22
-
I am testing on Xiaomi mi4 – Priyanshu Goyal Apr 21 '16 at 18:01
-
Now I tested it on OnePlus X. It works even if the RAM is cleared on this device. Then I tested it on Le1s, again the app doesn't work if its cleared from RAM. Any ideas as to how to solve this? – Priyanshu Goyal Apr 21 '16 at 18:18
-
Xiaomi has customized Android features which is device specific. Check my answer below for the solution. I wish it helps you! – zeeali Apr 22 '16 at 05:48
4 Answers
In Xiaomi devices, you just have to add your app to Autostart list, to do so, follow these simple steps given below:
Open Security app on your phone.
Tap on Permissions, it'll show you two options: Autostart and Permissions
Tap on Autostart, it'll show you list of apps with on or off toggle buttons.
Turn on toggle of your app, you're done!

- 1,524
- 20
- 31
-
It worked! but whenever i restart the phone, it agains takes away the autostart permission from the app. How to give it the permission permanently? – Priyanshu Goyal Apr 22 '16 at 16:30
-
I checked it on my Xiaomi Redmi 1s, it doesn't take away the permission, it's still there in autostart list. You'll have to manage the restart using a boot broadcast receiver. – zeeali Apr 23 '16 at 05:17
-
@Sidhartha We cannot automate this workaround, it is device specific settings. You must guide your app users what to do (just like permissions) to get your app running well in their devices. – zeeali Jul 11 '17 at 06:42
-
@zeeali other application like alarm is working without this kind of setup ,they would be adding some permission !! – Sidhartha Jul 11 '17 at 06:52
You can register broadcast receivers either inside the activity at runtime or in the manifest. You want to adopt the latter approach

- 530
- 5
- 8
-
The receiver is declared in the manifest. But still the receiver doesn't work when the app is cleared from the RAM. – Priyanshu Goyal Apr 21 '16 at 18:09
In the past I have similar problems with AlarmManager, AlarmReceivers and this kind of things. There are some tips that can help you in your code:
- Make sure that you are scheduling your alarm correctly.
- Make sure that you are setting the propers permissions on the manifest.
- Take care if the device is locked or it was rebooted.
There is a quite useful tutorial that helps me to control and make a "Hello World!" example with AlarmManager: AlarmManager Repeating Example
Note: In API 19 and higher, the method setRepeating is not exactly (maybe the alarm triggers at 10:00 or at 10:15), so you must use setExact.
Hope it helps!

- 4,085
- 3
- 26
- 34
-
The alarm is working perfectly until n unless the user explicitly clears it from the ram. So, I assume that there needs to be a service that is to be used so that somehow the receiver keeps working in the background? – Priyanshu Goyal Apr 21 '16 at 18:02
You can register broadcast receiver in two ways
1. From your activity.
2. From your manifest.
if you registered broadcast throught activity, It wont receive after your activity is destroyed, So thats is where we register BroadcastReceiver
in manifest.
This link will help you BroadcastReceiver

- 612
- 1
- 5
- 16
-
Yes, I had registered it in manifest. The alarm doesnt work if the application is cleared from the RAM by the user from the multitasking window. If the alarm is present on the RAM, it works perfectly well. – Priyanshu Goyal Apr 21 '16 at 18:04