How keep services alive when the device turned off? - android

There is a way to keep the service running even if the device turned off!??
(like a Alarm Clock still working even the device off or no battery or the clock setting still).
I was looking for some information on this question and could not find even a hint. If someone could answer me this it would really help me and I would be grateful

You can't. Instead you restart the service at boot time, by registering a BOOT_COMPLETE broadcast receiver in your manifest.

Related

Broadcast receiver not working when app is cleared from RAM

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.
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!
You can register broadcast receivers either inside the activity at runtime or in the manifest. You want to adopt the latter approach
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!
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

What can android listen for while asleep?

I can't find a list of events that can be listened for while a device is sleeping.
For example, it is possible to use LocationListener while asleep.
My goal is to detect when a user is in his car by at least one of the
following methods, which will then wake the phone:
NFC tag on car dock
Bluetooth-enabled car radio in range
But failing those, I'm open to ideas.
Thanks
The best idea so far is to wake the phone periodically as per this link:
Android - periodically wake up from standby mode?
As per the documentation, I could wake the phone up and just use the CPU, while keeping the screen off.
But there might be disadvantages, perhaps there is a better way.
You should create a brodcast receiver and register specific actions.
An example to detect a boot: Android -Starting Service at Boot Time.

Android service that was running when the phone was shut down starts automatically when the phone is booted

I am currently developing an Android telephony application that includes a service to handle all the SIP signaling for making and receiving calls.
I want this service to start exclusively when the user has correctly logged into the application. However, I am observing an undesired behavior: if the device is shut down while the app is running, the service is automatically started after the phone boots. This does not happen if the application is closed at the moment of shutting down the phone.
I have been reading about it but no answer comes up. Could anybody explain why this happens and how to prevent it?
Thank you in advance.
Thanks to CommonsWare comment I have quickly found the answer:
[...] The only way a service starts up is if somebody starts it, and the OS will not do that on its own.
I was so blinded thinking the OS was responsible for it that I didn't notice it was being done on purpose, as an undocumented feature inherited from a former version of the app.
There was a BroadcastReceiver listening to the android.intent.action.BOOT_COMPLETED action. This receiver was, among other things, restarting the service on start up when the app had not been properly shut down.
Thank you CommonsWare for your help.
Update
After preventing the BroadcastReceiver from listening to the BOOT_COMPLETE action, I still experience the same behavior.
The reason is that this BroadcastReceiver is also listening to connectivity changes to restart the SIP service when the WIFI or a data connection becomes active, only when the app is running. Wether the application was closed or not is stored in the app preferences, but this value was not properly set when the phone was shut down while the app was running.
That is why the service was still unwantedly starting on boot: because the BroadcastReceiver detected an android.net.conn.CONNECTIVITY_CHANGE at start up and the preference telling wether the app was still running or had been quit was not properly updated.

Invoke a Service even when the Device is Switched Off

I am working on a Security application, where i need to play siren music on receiving certain text(say "siren").
So far, I've been able to receive the SMS intent and play the .mp3 siren music. But the problem encounters when the device is switched off.
Its been to my knowledge that there are some Intents(dont know what exactly to call them), those intents are fired even when the Device is switched off, just like the Scheduled Alarms (which executes even when the phone is switched off)
If anyone knows about those services or whatever they are, it would be helpfull if you share those ideas.
If you need device to be awaken when you do your tasks, then simply wake it up using PowerManager. Please note that once your onReceive() is completed device may fall back to sleep, so if you spawn some other processed in your BroadcastReceiver, you need to hold WakeLock.
You may take a look at this as well: https://github.com/commonsguy/cwac-wakeful
Maybe this might help.. Shouldn't be that hard to do but how can you get a text message if the phone is turned off?!
http://developer.android.com/reference/android/app/AlarmManager.html

C2DM Broadcast receiver stops being called after some time

I have a working broadcast receiver for C2DM, which normally works perfectly, but in some cases in the field, it just stops being invoked by the system. My question is:
Under what circumstances would Android suddenly stop sending intents to a particular application, even if the app is currently running?
Given that it works the majority of the time, I'm trying to figure out if there's some way for the broadcast receiver to "go bad" or the process be marked bad etc. I have verified that Google is happy with the C2DM (i.e. it's not rate-limited due to quota or anything like that).
I am having exactly the same problem and what helps me is toggling wifi on and off, when it reconnects it starts receiving again. For me this is only the case when using C2DM, not tested with other broadcastreceivers. I know that this is not really an exact answer to the problem.
There also happens to be a problem with C2DM not working very good over wifi. So are you using your wifi connection or mobile-internet? Try only using your mobile internet for a while and see if the receiver still stops receiving.
Let me know if one of these tips got you any further!
Dino
just change one line in manifest.
receiver android:name=".MyBoardCastReceiver" android:enabled="true"

Categories

Resources