Hopefully an easy one for someone, but causing me problems.
I have an app that loops sound files. I have no code in onPause as I want the app to continue playing music in the background.
I now need the app to close if an incoming call is received.
I can add code to onPause and close the activity and stop the mediaplayer. However this stops the app if any app is launched.
How can I detect an incoming call to check for inside onPause?
To detect the incoming call, we register a BroadcastReceiver for the
action android.intent.action.PHONE_STATE. This will be broadcasted
when there is a change in phone state. The receiving intent will have
an extra string variable TelephonyManager.EXTRA_STATE which describes
the phone state. If this state is TelephonyManager.EXTRA_STATE_RINGING
then there will be another extra string variable
TelephonyManager.EXTRA_INCOMING_NUMBER. This variable contains the
incoming phone number. Note that this variable will not be present
when the state is not TelephonyManager.EXTRA_STATE_RINGING.
source : http://www.androiddevblog.net/android/detecting-incoming-and-outgoing-calls-in-android
basically, once you register a broadcast receiver you can listen for the broadcast notification you are interested in your phone, and then act accordingly.
You do not have to. Android takes care of it pausing your app - you have to use telephony manager to determine whether to stop your aplication or not. Proper way would be to handle this in onPause() method
Related
I am developing an app and this app needs to give a clear indication to the user when some event happens.
Only thing I could do until now is giving a notification in the notification area. But, I need to give a more visible notification, similar to the behavior when phone is ringing in an incoming call.
As I can understand, the reason why android is only allowing apps to give a notification is to prevent apps from disturbing the user. But, this app I am developing plays a vital role in the job of the user, so I don't think it is inappropriate to give a such strong notification.
I know it should be doable since apps like Viber can start an activity similar to a incoming phone call, even when the device is sleeping.
Does anyone know how to get this done?
Register a broadcast receiver, and add a custom action to it say CustomAction.Instead of showing notification, throw a broadcast and add CustomAction via intent filter.
Now in the onReceive method of broadcast listener, check
if(intent.getAction.equals("CustomAction"))Intent i = new Intent(context, YourActivity);
context.startActivity(i);
Sorry for not a formatted answer, I'm driving, will update it later for more clarification.
Update
Register broadcast receiver in a sticky service. So that service can be started automatically if killed and register broadcast register again.
Don't forget to unregister broadcast receiver in onDestroy() method of service and also in YourActivity when you purpose is resolved.
Just adding a sticky service (which does nothing) fixed the issue. Adding the service prevented the process getting killed when user exits the app and removes it from recent app list.
Because of the service, the app process is running even when a no UI is visible. In this state, if an activity is shown from the GCM service, it gets shown.
You can trigger a broascast as Vinay mentioned. If it still does not work, try using wake-locks. These wake-locks help in waking the device when it is in sleep mode. It will act like force wake and after calling wake-locks, you can perform your actions.
Hope it helped..
Thanks.
I have an android app which waits for incoming calls to arrive and then (when the call state changes (I'm using the telephony manager class to detect incoming calls and its working great)) my app does something.
The problem is, if my app is running and listening for calls in the background, and then I open some other heavy apps and using them massively, my app gets paused by the system and the user must re-open it in order to resume listening for phone calls.
Is there a way to keep my app waiting for calls as long as the user didn't press on the "stop listening" button inside my app ?
Thanks.
Maybe this wil help you ,i had the same thing and i solved it with this answer
Use Broadcast Receiver to receive telephony broadcasts, which you have to register in a background Service. This way, the system will automatically notify your app about the events and also restart the app if it's not already running (of course, it won't happen if the app was force closed).
I am researching whether it is possible to receive an incoming mobile phone call and intercept the call within an application so that before we accept the call we can start to play some music in the background such that the caller listen to the music as well.
Of course you can ! You will have to create service that listens to incoming call intent and then do what you like.
See this tutorial for more information:
How To Handle Incoming Calls in Android
I am using a broadcast receiver in my app, to display a photo on BOOT_COMPLETED. However, I noticed that if I receive a phone call on my phone, the photo is displayed in front of incoming phone call activity, so I can's see who is calling me and what is worse - can't answer the phone.
What is the best way to override this behavior?
you could add another mechanism for listening to phone calls events, and if you detect that the phone is ringing (or the call was answered), you won't show the activity...
in any case, please don't show such things. no user likes popups go out of nowhere.
instead, use notifications to tell the user something has happened.
I have an app on Android that reacts to incoming calls.
Now, since the OS shuts my app down whenever it want,
I need a way to to listen to the incoming calls and launch the app when it happens.
Will a BroadcastReceiver help? (just like launching on device restart)
Any idea?
thanks!
I think you have answered your own question. This is just the sort of thing a broadcast receiver is meant for. If the receiver is registered in your manifest then the application does not have to be running.
It will be automatically started when a matching intent is broadcast. Typically the response will be to update content or activities, make notifications with the Notification manager or launch/manipulate services.
Note that there is a 5 second execution limit in the BroadcastReceiver onReceive handler to ensure you do not try to do any 'heavy lifting' in it. Exceed this and a force close dialog will be displayed.
Yes a BroadcastReceiver would to the job as it will fire even you app is not running.