我怎样才能确定是否有呼叫振铃"线路2英寸(例如呼叫等待)
我使用一个意图过滤器来听变化PHONE_STATE
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)
...但我怎么能确定它是否是1号线或2号线正在振铃?
... 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.
我找到了一种方法来实现这个......张贴了未来的家伙。
I found a way to implement this... posting it for "the next guy".
在简单地说,三个国家之间的手机状态招式:
In a nutshell, the phone state moves between three states:
- 在空闲 - 你不使用手机
- 在RINGING - 一个电话呼入
- 在OFF_HOOK - 这款手机被打爆
在化铃声状态广播,它是紧跟一个空闲或OFF_HOOK的状态恢复到什么是pre-呼入呼叫。
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;
}
}
}