我正在构建一个与股票拨号器(Marshmallow API)挂钩的应用程序。我的目标是获取传入和传出呼叫,同时获取Connection对象的句柄以操作 Connection 的方法。
我已经注册PhoneAccount
了CAPABILITY_CALL_PROVIDER
。
PhoneAccount.Builder builder = new PhoneAccount.Builder(phoneAccountHandle, "CustomAccount");
builder.setCapabilities(PhoneAccount.CAPABILITY_CALL_PROVIDER);
PhoneAccount phoneAccount = builder.build();
telecomManager.registerPhoneAccount(phoneAccount);
我的帐户在股票拨号器应用程序中可见(设置-> 通话-> 通话帐户)并且我已启用它。
我有一个监控电话状态的服务,并在CALL_STATE_RINGING
它上面调用 TelecomManager 的addNewIncomingCall()
方法。
public void onCallStateChanged(int state, String incomingNumber) {
if (state == TelephonyManager.CALL_STATE_RINGING) {
Toast.makeText(getApplicationContext(), "Phone Is Ringing",
Toast.LENGTH_SHORT).show();
Bundle extras = new Bundle();
Uri uri = Uri.fromParts(PhoneAccount.SCHEME_TEL, incomingNumber, null);
extras.putParcelable(TelecomManager.EXTRA_INCOMING_CALL_ADDRESS, uri);
extras.putParcelable(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE, phoneAccountHandle);
telecomManager.addNewIncomingCall(phoneAccountHandle, extras);
}
if (state == TelephonyManager.CALL_STATE_OFFHOOK) {.......}
...
}
我的自定义连接服务:
@Override
public Connection onCreateIncomingConnection(PhoneAccountHandle connectionManagerPhoneAccount, ConnectionRequest request) {
Toast.makeText(getApplicationContext(), "onCreateIncomingConnection called", Toast.LENGTH_SHORT).show();
Connection incomingCallCannection = createConnection(request);
incomingCallCannection.setRinging();
return incomingCallCannection;
}
@Override
public Connection onCreateOutgoingConnection(PhoneAccountHandle connectionManagerPhoneAccount, ConnectionRequest request) {
Toast.makeText(getApplicationContext(), "onCreateOutgoingConnection called", Toast.LENGTH_SHORT).show();
Connection outgoingCallConnection = createConnection(request);
outgoingCallConnection.setDialing();
return outgoingCallConnection;
}
private Connection createConnection(ConnectionRequest request) {
mConnection = new Connection() {
@Override
public void onStateChanged(int state) {
super.onStateChanged(state);
}
@Override
public void onDisconnect() {
super.onDisconnect();
mConnection.setDisconnected(new DisconnectCause(DisconnectCause.CANCELED));
mConnectionsAvailableForConference.clear();
mConnection.destroy();
}
@Override
public void onSeparate() {
super.onSeparate();
}
@Override
public void onAbort() {
super.onAbort();
mConnection.setDisconnected(new DisconnectCause(DisconnectCause.CANCELED));
mConnection.destroy();
}
@Override
public void onHold() {
super.onHold();
}
@Override
public void onAnswer() {
super.onAnswer();
mConnection.setActive();
}
@Override
public void onReject() {
super.onReject();
mConnection.setDisconnected(new DisconnectCause(DisconnectCause.CANCELED));
mConnection.destroy();
}
};
mConnection.setAddress(request.getAddress(), TelecomManager.PRESENTATION_ALLOWED);
mConnection.setExtras(request.getExtras());
return mConnection;
}
现在,两个 ConnectionService 的回调方法分别在传入和传出调用中调用。问题是,当我去拨号器并拨出电话(使用 my PhoneAccount
)时,我会看到拨号屏幕(inCallUI ?),显示正确的来电者信息(联系人姓名、电话号码等),但线路我的听筒没有响铃并且没有建立呼叫(应该接听电话的电话号码没有响铃)。
我尝试super.onCreateOutgoingConnection(PhoneAccountHandle connectionManagerPhoneAccount, ConnectionRequest request)
在回调中返回而不是创建我自己的 Connection 对象,我得到了相同的行为。
TLDR:我的应用程序与拨号器通信,能够拨打电话并显示拨号屏幕,但电话线不响,也没有任何反应。