Invoke a Service even when the Device is Switched Off - android

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

Related

Android SMS receiving and handling

I know that it is possible to handle incoming sms on android and I think even me as a beginner can do that. But my question is: Will the app also run when the device is locked? I am working on an application that sends an email with the text and sender to a specific email address when the device received a SMS. But it also has to work when the device locked itself after a few minutes? Whats the best way to do that or is it already working by using the onRecieve method?
Thanks for any kinda help and please be kind I am quite new to programming :D
It's complicated...
As soon as an app is paused (that means : not displayed on the screen), it could be destroyed by the Android system to preserve battery or reduce CPU / RAM usage.
So : no, you have no guarantees the app will still be alive.
You can set a BroadcastReceiver to your AndroidManifest.xml and create a BroadcastReceiver class in your app. The onReceive() method will be called and the code you set in your class will be executed. Even if the app is not running at the moment the SMS is received.
But there's another issue : Deep Sleeping. To preserve battery, Android will turn off any battery-intensive systems when the device is not used for many hours. Battery-intensive systems includes : Wi-fi and Data. SMS is excluded from this list (but some constructors may include an option to disable SMS receptions when in Deep Sleeping, in this case, you have no option, just warn the user to not disable SMS receptions in Deep sleeping), gracefully.
That means implementing the onReceive() method will not be sufficient. You will need to wake up the device to enable Wifi and Data, allowing you to send an email.
So, to avoid this problem, extends a WakefulBroadcastReceiver. This is like a "normal" broadcast receiver, but it will wake up the device, and let it sleep again when the code is fully executed.

How keep services alive when the device turned off?

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.

how to make server call whenever device screen is turned ON without Service

This requirement is only satisfied if the app is running in the background.And if the Screen is turned on if the user presses to check any notifications then an Asynctask is called if the app is running in the background and makes a call to the server.
I have tried using Broadcast Receiver when screen on and tried to execute, it works only if the app is on the front screen after pressing the home button .And then if the user presses Power button after an hour then nothing happens .
Basically I am not sure if the app is being killed after sometime when in background. Please help me.I am a noob in Android and this functionality is something I thought most of the developers might be using but I did not see anything except service calls and I really did not want any service/alarm-manager as I don't want it to work continuously.
TIA
how to make server call whenever device screen is turned ON without Service
This is not possible. ACTION_SCREEN_ON is a broadcast that can only be received by a BroadcastReceiver registered via registerReceiver(). So, unless you are the foreground activity, the only way you can receive this broadcast is via an always-running service, which is not a good idea.
as I don't want it to work continuously
Then do not "make server call whenever device screen is turned ON". Find some other solution for whatever business problem you have that you are trying to solve this way.

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"

Android: creating an insistent alarm (ex: morning alarm, incoming call)

I'm writing an alarm-based application, and I'm looking for a way to wake up the user insistently. Ideally, I'd like the phone to vibrate, ring, and display a message. I tried a few different options, here's what I have for the moment:
Have the background service start an activity that vibrates & plays music.
problem: even with a WAKE_LOCK, it seems that the user still has to turn on the screen and unlock it to see my activity
Use the AlarmManager with RTC_WAKEUP and a broadcast receiver to start the aforementioned activity
same issue
Simply pop up a notification, which the user can click to see the activity
problem: stil the same issue, and also I can't get the notification to vibrate and ring, and repeat often until dismissed
What I would really like is to have a behaviour similar to the basic Android morning alarm, or similar to receiving a phone call: regardless of whether your phone is awake or sleeping, it rings, vibrates and displays information such as a message, a photo...
Any tips on the direction to use would be greatly appreciated! I'm sure I've seen alarm apps or "fake phone call" apps do something similar, but I'm out of ideas.
Cheers
ps: in the context of this application, I think it would be reasonable to wake up the user this way. A discreet notification would not be appropriate - unless you can wake up to a single vibration :)
Have you looked at the ACQUIRE_CAUSES_WAKEUP flag? It looks like it should do what you want. See here for more information: http://developer.android.com/reference/android/os/PowerManager.html

Categories

Resources