无法检测时,呼出的Android正在回答 [英] Cannot detect when outgoing call is answered in Android
问题描述
要检测时,拨出电话接通后,我试图创建一个 PhoneStateListener
和监听 TelephonyManager
的 CALL_STATE_RINGING
, CALL_STATE_OFFHOOK
和 CALL_STATE_IDLE
,从的this质疑,但它似乎并没有工作,如下所述。
To detect when an outgoing call is answered, I tried creating a PhoneStateListener
and listening for TelephonyManager
's CALL_STATE_RINGING
, CALL_STATE_OFFHOOK
, and CALL_STATE_IDLE
, from this question, but it does not seem to work, as explained below.
首先,我注册清单中的以下权限:
First, I registered the following permission in the manifest:
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
然后,的BroadcastReceiver
名为 OutCallLogger
映入 NEW_OUTGOING_CALL
每当呼出由事件:
Then, a BroadcastReceiver
called OutCallLogger
that catches the NEW_OUTGOING_CALL
event whenever an outgoing call is made:
<receiver android:name=".listener.OutCallLogger">
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
接下来,我执行 OutCallLogger
的。我成立了一个名为布尔 noCallListenerYet
来避免附加一个新的 PhoneStateListener
到 TelephonyManager
每当的onReceive()
被调用。
Next, my implementation of OutCallLogger
. I set up a boolean called noCallListenerYet
to avoid attaching a new PhoneStateListener
to the TelephonyManager
whenever onReceive()
is invoked.
public class OutCallLogger extends BroadcastReceiver {
private static boolean noCallListenerYet = true;
@Override
public void onReceive(final Context context, Intent intent) {
number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
if (noCallListenerYet) {
final TelephonyManager tm = (TelephonyManager) context.getSystemService(
Context.TELEPHONY_SERVICE);
tm.listen(new PhoneStateListener() {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
Log.d(This.LOG_TAG, "RINGING");
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Log.d(This.LOG_TAG, "OFFHOOK");
break;
case TelephonyManager.CALL_STATE_IDLE:
Log.d(This.LOG_TAG, "IDLE");
break;
default:
Log.d(This.LOG_TAG, "Default: " + state);
break;
}
}
}, PhoneStateListener.LISTEN_CALL_STATE);
noCallListenerYet = false;
}
}
}
现在,当我拨出电话在我的设备, CALL_STATE_RINGING
永远不会被调用。我总是只能得到IDLE打印输出摘机时,其他线路开始振铃,没事的时候呼叫被应答,并打印输出空闲通话结束时再次。
Now, when I make an outgoing call in my device, CALL_STATE_RINGING
is NEVER invoked. I always only get printouts of "IDLE" to "OFFHOOK" when the other line starts ringing, nothing when the call is answered, and a printout of "IDLE" again when the call is ended.
我怎么能可靠地检测时呼出的回答是Android的,或者是说甚至有可能?
How can I reliably detect when an outgoing call is answered in Android, or is that even possible?
推荐答案
由于Android 5.0,这是可能的系统应用程序。但是,你需要使用隐藏的Android API。
Since Android 5.0 this is possible for system apps. But you need to use the hidden Android API.
我得到它的工作是这样的:
I got it to work like this:
<uses-permission android:name="android.permission.READ_PRECISE_PHONE_STATE" />
<receiver android:name=".listener.OutCallLogger">
<intent-filter>
<action android:name="android.intent.action.PRECISE_CALL_STATE" />
</intent-filter>
</receiver>
public class OutCallLogger extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
switch (intent.getIntExtra(TelephonyManager.EXTRA_FOREGROUND_CALL_STATE, -2) {
case PreciseCallState.PRECISE_CALL_STATE_IDLE:
Log.d(This.LOG_TAG, "IDLE");
break;
case PreciseCallState.PRECISE_CALL_STATE_DIALING:
Log.d(This.LOG_TAG, "DIALING");
break;
case PreciseCallState.PRECISE_CALL_STATE_ALERTING:
Log.d(This.LOG_TAG, "ALERTING");
break;
case PreciseCallState.PRECISE_CALL_STATE_ACTIVE:
Log.d(This.LOG_TAG, "ACTIVE");
break;
}
}
}
您可以在<一个所有可能的通话状态href="https://android.googlesource.com/platform/frameworks/base.git/+/master/telephony/java/android/telephony/$p$pciseCallState.java#26"相对=nofollow> preciseCallState.java 和意图包含所有演员<一href="https://android.googlesource.com/platform/frameworks/base/+/4f868ed/services/core/java/com/android/server/TelephonyRegistry.java#1242"相对=nofollow> TelephonyRegistry.java 。
You can find all possible call states in PreciseCallState.java and all extras that the intent contains in TelephonyRegistry.java.
这篇关于无法检测时,呼出的Android正在回答的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!