作者:别想着摆脱_525 | 来源:互联网 | 2023-01-31 10:29
我正在尝试实现ConnectionService,PhoneAccount和PhoneAccountHandle,以获取断开我拨出呼叫的原因(例如,用户拒绝了呼叫,无法接通等)。到目前为止,我已经能够使用customPhoneAccount发起呼叫,但是该电话从未经过,因此我无法得到答复。
这是我现在编写的代码:
register()是从我的活动中调用以注册phoneAccount的方法:
public void register() {
TelecomManager manager = (TelecomManager) getSystemService(TELECOM_SERVICE);
PhoneAccountHandle phOneAccountHandle= new PhoneAccountHandle(
new ComponentName(getPackageName(),
MyConnectionService.class.getName()), "myConnectionServiceId");
PhoneAccount.Builder builder = PhoneAccount.builder(phoneAccountHandle, "CustomAccount");
builder.setCapabilities(PhoneAccount.CAPABILITY_CALL_PROVIDER | PhoneAccount.CAPABILITY_CONNECTION_MANAGER);
PhoneAccount phOneAccount= builder.build();
manager.registerPhoneAccount(phoneAccount);
}
发起呼叫的call()方法:
public void call() {
TelecomManager manager = (TelecomManager) getSystemService(TELECOM_SERVICE);
PhoneAccountHandle phOneAccountHandle= new PhoneAccountHandle(
new ComponentName(getPackageName(),
MyConnectionService.class.getName()), "myConnectionServiceId");
Bundle test = new Bundle();
test.putParcelable(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE, phoneAccountHandle);
manager.placeCall(Uri.parse("tel:" + "1212312312"), test);
}
这是MyConnectionService:
public class MyConnectionService extends ConnectionService {
public static final String TAG = MyConnectionService.class.getName();
@Override
public int onStartCommand(Intent intent,int flags, int startId) {
Log.d(TAG, "On Start");
return super.onStartCommand(intent, flags, startId);
}
@Override
public Connection onCreateOutgoingConnection(PhoneAccountHandle connectionManagerPhoneAccount, ConnectionRequest request) {
Connection cOnnection= super.onCreateOutgoingConnection(connectionManagerPhoneAccount, request);
Log.d(TAG, connection.getDisconnectCause().getReason());
return connection;
}
@Override
public void onCreateOutgoingConnectionFailed(PhoneAccountHandle connectionManagerPhoneAccount, ConnectionRequest request) {
if (request != null) {
Log.d(TAG, request.toString());
}
super.onCreateOutgoingConnectionFailed(connectionManagerPhoneAccount, request);
}
我尝试了不同的功能以及与ConnectionService相关的其他线程上提到的所有选项,但无法获得所需的结果,有人可以在这方面帮助我吗?