سؤال

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