Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
327 views
in Technique[技术] by (71.8m points)

android - How can I determine if there is a call ringing on "line 2" (e.g. call waiting)

I'm using an intent-filter to listen to changes in PHONE_STATE

    <!-- Listen for phone status changes -->
    <receiver android:name=".IncomingCallReciever">
           <intent-filter>
               <action android:name="android.intent.action.PHONE_STATE" />
           </intent-filter>
    </receiver>

... and can easily detect an incoming call

       intent != null 
    && intent.getAction().equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED)
    && intent.hasExtra(TelephonyManager.EXTRA_STATE)
    && intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_RINGING)

... but how can I determine if it is line 1 or line 2 that is ringing?

My application needs to react ONLY when the user is currently on a phone call and another call is coming in.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I found a way to implement this... posting it for "the next guy".

In a nutshell, the phone state moves between three states:

  1. 'IDLE' - You're not using the phone
  2. 'RINGING' - A call is coming in
  3. 'OFF_HOOK' - The phone is off the hook

When a 'RINGING' state is broadcast, it is immediately followed by an IDLE or OFF_HOOK to restore the state to what it was pre-incoming-call.

Put it all together and you end up with this:

package com.telephony;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;

public class PhoneStateChangedReciever extends BroadcastReceiver {

    private static String lastKnownPhoneState = null;

    @Override
    public void onReceive(Context context, Intent intent) {
        if(intent != null && intent.getAction().equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED)) {

            //State has changed
            String newPhoneState = intent.hasExtra(TelephonyManager.EXTRA_STATE) ? intent.getStringExtra(TelephonyManager.EXTRA_STATE) : null;

            //See if the new state is 'ringing'
            if(newPhoneState != null && newPhoneState.equals(TelephonyManager.EXTRA_STATE_RINGING)) {

                //If the last known state was 'OFF_HOOK', the phone was in use and 'Line 2' is ringing
                if(lastKnownPhoneState != null && lastKnownPhoneState.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
                    ...
                }

                //If it was anything else, the phone is free and 'Line 1' is ringing 
                else {
                    ... 
                }
            }
            lastKnownPhoneState = newPhoneState;
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...