我正在使用intent-filter来收听PHONE_STATE中的更改
<!-- Listen for phone status changes -->
<receiver android:name=".IncomingCallReciever">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
Run Code Online (Sandbox Code Playgroud)
......并且可以轻松检测到来电
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)
Run Code Online (Sandbox Code Playgroud)
...但我怎样才能确定它是第1行还是第2行响铃?
我的应用程序只需在用户当前正在通话并且另一个呼叫进入时才需要做出反应.
Sky*_*ton 13
我找到了一种方法来实现这个...发布它为"下一个人".
简而言之,手机状态在三种状态之间移动:
当广播"RINGING"状态时,紧接着是IDLE或OFF_HOOK,以将状态恢复到预呼入状态.
把它们放在一起,你最终得到这个:
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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5768 次 |
| 最近记录: |