Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8112403
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T02:29:27+00:00 2026-06-06T02:29:27+00:00

Possible Duplicate: Stopping & Starting music on incoming calls I have a simple application

  • 0

Possible Duplicate:
Stopping & Starting music on incoming calls

I have a simple application that plays some audio using the MediaPlayer class. While debugging my application I found that the audio still plays during incoming and outgoing calls.

I found that the solution to my problem is using a phoneStateListener. I don’t quite understand how to do this though. I saw some examples that used a BroadcastReceiver along with the PhoneStateListener and I couldn’t understand why we needed a BroadCastReceiver.

I would appreciate it if someone could provide a sample implementation of the PhoneStateListener. If I can get the phone state I could pause or resume the audio because my MediaPlayer object is global for the entire application.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T02:29:30+00:00Added an answer on June 6, 2026 at 2:29 am

    This is what I use…
    The onCallStateChanged() function gets called in all PhoneStateListeners anytime the call state has changed, so that’s what you need to handle.

    import android.telephony.PhoneStateListener;
    import android.telephony.TelephonyManager;
    
    //Makes sure the audio is paused for incoming/outgoing phone calls
    public class ListenToPhoneState extends PhoneStateListener {
    
        private boolean pausedForPhoneCall = false;
        private UIManager uiManager;
    
        ListenToPhoneState(UIManager manager){
            uiManager = manager;
        }
    
        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
    
            switch (state) {
                case TelephonyManager.CALL_STATE_IDLE:
                    resumeInAndroid();
                    return;
                case TelephonyManager.CALL_STATE_OFFHOOK: 
                    pauseInAndroid();               
                    return;
                case TelephonyManager.CALL_STATE_RINGING: 
                    pauseInAndroid();               
                    return;
            }
        }
    
        private void resumeInAndroid(){
            if(pausedForPhoneCall == true) {
                pausedForPhoneCall=false;
                uiManager.waitForPhoneCall(false);
            }
        }
    
        private void pauseInAndroid(){
            if(pausedForPhoneCall == false){
                pausedForPhoneCall=true;
                uiManager.waitForPhoneCall(true);
            }
        }
    
        String stateName(int state) {
            switch (state) {
                case TelephonyManager.CALL_STATE_IDLE: return "Idle";
                case TelephonyManager.CALL_STATE_OFFHOOK: return "Off hook";
                case TelephonyManager.CALL_STATE_RINGING: return "Ringing";
            }
            return Integer.toString(state);
        }
    }
    

    If you want to add this to your current Activity, do this..

    ListenToPhoneState listener = new ListenToPhoneState(userInterface);
            TelephonyManager tManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
            if(tManager != null)
                tManager.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
    

    Wouldn’t you rather just pause your current activity? If you pass your class an instance of the current activity, you could call finish() and create a new activity right in the switch statement if you really want to.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Visual studio keeps building everything I have a project that includes an
Possible Duplicate: Logcat not displaying my log calls I am developing an Android Application,
Possible Duplicate: Stopping GIF Animation Programmatically When I have a page open with an
Possible Duplicate: Yield In VB.NET In C#, when writing a function that returns an
Possible Duplicate: iOS - Detecting whether or not device support phone calls? I'm writing
Possible Duplicate: Is there a barcode recognition framework for iOS? I've done some iPhone
Possible Duplicate: regex for URL including query string I have a text or message.
Possible Duplicate: How to call a JavaScript function from PHP? I have a php
Possible Duplicate: Paypal Checkout Express empty cart problem i have an issue with paypal
Possible Duplicate: Extracting dollar amounts from existing sql data? I have a column in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.