I have a simple BroadcastReceiver set up to do something when the user gets an incoming SMS. But I need it to run in the background and when the device is asleep. So would I use a Service that starts the BroadcastReceiver? If so, can someone give me some pseudo-code? And how would this work if the device is asleep?
I have a simple BroadcastReceiver set up to do something when the user gets an incoming SMS.
OK.
But I need it to run in the background and when the device is asleep.
Not really.
So would I use a Service that starts the BroadcastReceiver?
No. Your BroadcastReceiver should be in the manifest, so it can be invoked regardless of whether any of the rest of your code is running. That's why I say "not really" to "run in the background" -- you DO NOT WANT code running all the time in the background. Rather, you want to be able to receive broadcasts at any point, and that is what putting the receiver in the manifest is for.
If so, can someone give me some pseudo-code?
https://github.com/commonsguy/cw-advandroid/tree/master/SMS/Monitor
And how would this work if the device is asleep?
It won't. However, an incoming SMS, like an incoming phone call, will wake up the device.
Related
I'm using Agora RTC and Signaling SDK for building a video call function.
But I get a problem that if the receiver side exits app suddenly (like swiping app out from recent tray).
Then, the caller side is still on calling screen and can not get any event from receiver side to end the video call.
Please help
to solve this problem, you need to add leave channel logic at onDestroy method in receiver's activity. So that when the receiver exits the app, he will leave the channel and that will notify the caller.
I need to create an android service that:
Starts whenever the screen is on (whether it is at boot time or not)
sends a notification every 20 minutes (if the screen is on)
stops whenever the screen is off
Every tutorial I've read uses an activity, but I need this to be a service because the app is not supossed to be running other than when the user wants to change a setting. The documentation says I need an IntentService, but I cannot stop that manually and I cannot use a Service because it is a long running operation. I tried with an alarm manager but it didn't worked, I don't even bother to show you the code because I really don't understand it. I do not know how to make the service check if the screen is on or not, if I use a BroadcastReceiver it won't be inmediately processed so I am just stuck
To implement your requirement. You need 3 things such as Service, BroadcastReceiver & AlarmManager :
AlarmManager [which will fire after every 20 minutes]
Service [which will make changes like showing notification as system gets notifies after every 20 minutes for your particular msg]
BroadcastReceiver [which will check for screen on/off right from booting to shutting down]
Refer these links :
http://thinkandroid.wordpress.com/2010/01/24/handling-screen-off-and-screen-on-intents/
http://androidexample.com/Screen_Wake_Sleep_Event_Listner_Service_-_Android_Example/index.php?view=article_discription&aid=91&aaid=115
So, I have a service for my app that is always running if the user turns it on. It listens with a broadcast receiver for USER_PRESENT to show a message. It is only supposed to show a message on unlocking.
The problem is though, when Android runs out of memory and kills it, then restarts it, it will show the message again, even if the user hasn't just unlocked their device. Is there a way to know who called the service?
I use service.START_NOT_STICKY, but would service.START_STICKY be better for this job? I guess I don't fully understand the differences but I'm pretty sure I want NOT_STICKY.
You can set the action field in the intent to a specific string when you call your receiver and then check it in onReceieve.
If it has your string then you called it , otherwise someone else.
When my phone receives a text message, it will upload it to my database. On the tablet I should get a notification about the new message as soon as possible.
How do I efficiently check for new messages from the database? I don't think a background process that queries the database every few seconds is efficient at all. It will drain the battery and it's huge waste of network.
Have a static BroadcastReceiver in your App as a listener. If a text message comes in the receiver will get started and onReceive() will get called. Now you can invoke a Service/ an Activity to save it to your database and put up a notification.
Here's a nice tutorial to get you started.
I want to create an application that can detect incoming calls and start my custom activity after a certain number of beeps (rings), I mean after 2 or 3 or 5 beeps (rings) my activity is triggered. How can I do it?
Thanks
I don't think you can count the number of rings the phone made since the start of the incoming call. There can't be a definitive measure of a single ring because the user can easily change the ringtone to a non-repetitive tune, for example, a song.
What you can do, however, is count the amount of time that passed since the arrival of the call. Set up a BroadcastReceiver for PHONE_STATE (you will need the corresponding permission in the manifest to receive the event). Once you receive the EXTRA_STATE_RINGING that came with the PHONE_STATE, set an alarm via the AlarmManager that will fire a Service that checks if EXTRA_STATE_OFFHOOK (broadcast when the call is picked up) has been broadcast after your waiting time. If not, then you can start your answering machine.
I have written a quick tutorial in my website on how to catch the call's arrival (when the phone rings), when the call is picked up, and when it ends.
My article about detecting incoming and outgoing calls, with the step-by-step instructions:
Detecting incoming and outgoing phone calls on Android
When you detect incoming call, you can start a timer, with interval equal to beepInterval * beepCount. And launch activity on this timer.