문제

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top