I have implemented an Activity that plays media from a URL in android
In order to add pause functionality when the incoming call is incoming I created a receiver that sets a a variable when the call is coming. The activity reads this variable and then pauses the music in its onPause() method and resets is when the call is done and the activity resumes the music in its onResume() method
This works fine as long the activity has the focus. If I go back to home screen while the music is playing, and then the call comes, the activity's onpause is not called & hence i can' stop & start the music
What is the way out for this? Anybody implemented a media player so that it intercepts incoming & outgoing calls at all the times & stops and starts the music correctly?
IMHO better way is described in off docs: http://developer.android.com/training/managing-audio/audio-focus.html
I think requestAudioFocus() should be able to handle this case automatically. You don't need to check call state explicitly.
Audio Focus is cooperative in nature. That is, applications are expected (and highly encouraged) to comply with the audio focus guidelines, but the rules are not enforced by the system. If an application wants to play loud music even after losing audio focus, nothing in the system will prevent that. However, the user is more likely to have a bad experience and will be more likely to uninstall the misbehaving application.
To request audio focus, you must call requestAudioFocus() from the AudioManager, as the example below demonstrates:
There are a few things you can do:
First of all, you can listen for changes in the call state using a
PhoneStateListener
. You can register the listener in the TelephonyManager:Remember to unregister the listener when it's no longer needed using the
PhoneStateListener.LISTEN_NONE
:For more information read the documentation.
Another thing you can do is listening for the broadcast
android.intent.action.PHONE_STATE
. It will contain the extraTelephonyManager.EXTRA_STATE
which will give you information about the call. Take a look at the documentation here.Please note that you'll need the
android.permission.READ_PHONE_STATE
-permission in both cases.OR - You can try a Receiver Application.
Create a Receiver named CallRecord.java
Then Add this line in manifest.xml file to register it on the App
I think that AudioManager is the best and fast solution. Here there is my implementation example:
I hope it's helpful for you :-)
For me idle state was coming while there was incoming call, the quick fix is to check in the broadcast receiver