我正在开发一个钩子到股票拨号器(Marshmallow API)的应用程序。我的目标是在获取来电和拨出电话的同时,掌握 Connection对象以操作连接的方法。
我已经注册了
现在,ConnectionService的回调方法分别在呼入和呼出通话时都被调用。问题是,当我进入拨号器并拨打一个外部电话(使用我的
我已经注册了
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的回调方法分别在呼入和呼出通话时都被调用。问题是,当我进入拨号器并拨打一个外部电话(使用我的
PhoneAccount
)时,我会看到拨号屏幕(在通话UI中?),正确的呼叫者信息被显示出来(联系人姓名、电话号码等),但是我的听筒里没有响铃声,通话也没有建立(应该接收呼叫的电话号码也没有响铃)。我尝试在回调中返回super.onCreateOutgoingConnection(PhoneAccountHandle connectionManagerPhoneAccount, ConnectionRequest request)
而不是创建自己的Connection对象,但我得到了相同的行为。简而言之:我的应用程序与拨号器通信,能够拨打电话并显示拨号屏幕,但电话线路没有响铃,什么也没有发生。