我正在使用 TelecomManager、ConnectionService 和 Connection 构建基本的呼叫应用程序。但是,当有来电时,我的incomingActivity UI 没有显示。以下是到目前为止的示例代码。
在我的 MainActivity.java
Intent intent = new Intent(TelecomManager.ACTION_CHANGE_DEFAULT_DIALER);
intent.putExtra(TelecomManager.EXTRA_CHANGE_DEFAULT_DIALER_PACKAGE_NAME, getPackageName());
startActivity(intent);
// ================================================================
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
TelecomManager manager = (TelecomManager) getSystemService(TELECOM_SERVICE);
// new ComponentName(getPackageName(), CallHandler.TAG), "myCallHandlerId");
PhoneAccountHandle phoneAccountHandle = new PhoneAccountHandle(
new ComponentName(getApplicationContext(), CallHandler.TAG), "myCallHandlerId");
PhoneAccount phoneAccount = PhoneAccount
.builder(phoneAccountHandle, "myCallHandlerId")
.setCapabilities(PhoneAccount.CAPABILITY_SELF_MANAGED)
.build();
manager.registerPhoneAccount(phoneAccount);
Log.i("Phone Account", "" + manager.getPhoneAccount(phoneAccountHandle));
Log.i("Phone Account", "" + manager.getPhoneAccount(phoneAccountHandle).isEnabled());
Log.i("Phone Account", "" + manager.getPhoneAccount(phoneAccountHandle).getClass());
Log.i("Phone Account isEnabled", "" + phoneAccount.isEnabled());
Bundle bundle = new Bundle();
Uri uri …Run Code Online (Sandbox Code Playgroud) java connection android telecom-manager android-connectionservice
我想为 Voip 拨号器应用程序实现“android.telephony”库。因此,根据此https://developer.android.com/reference/android/telecom/ConnectionService我正在尝试注册 PhoneAccount。我正在做如下,
private void registerPhoneAcc(String calleeURI, String phoneNumber) {
TelecomManager telecomManager = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
telecomManager = (TelecomManager) getContext().getSystemService(Context.TELECOM_SERVICE);
}
ComponentName componentName = new ComponentName(getContext().getPackageName(), String.valueOf(MyConnectionService.class));
PhoneAccountHandle phoneAccountHandle = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
phoneAccountHandle = new PhoneAccountHandle(componentName,"App" );
PhoneAccount phoneAccount = PhoneAccount.builder(phoneAccountHandle, "Primary").setCapabilities(PhoneAccount.CAPABILITY_CALL_PROVIDER).build();
telecomManager.registerPhoneAccount(phoneAccount);
}
}
Run Code Online (Sandbox Code Playgroud)
<service android:name=".app.MyConnectionService"
android:permission="android.permission.BIND_TELECOM_CONNECTION_SERVICE">
<intent-filter>
<action android:name="android.telecom.ConnectionService" />
</intent-filter>
</service>
Run Code Online (Sandbox Code Playgroud)
尽管我已经包含了许可,但我遇到了以下异常
W/System.err: java.lang.SecurityException: PhoneAccount connection service requires BIND_TELECOM_CONNECTION_SERVICE permission.
2021-02-03 13:58:39.259 24630-24630/com.ex.voip W/System.err: at android.os.Parcel.readException(Parcel.java:2013)
2021-02-03 …Run Code Online (Sandbox Code Playgroud) 我需要拦截设备框架发出的传出呼叫的事件。
按照android 指南,我停在第 3 点电信子系统绑定到您的应用程序的 ConnectionService 实现。,也就是说我已经走到了这一步:
通话流程
val telecomManager :TelecomManager= getSystemService(
TELECOM_SERVICE
) as TelecomManager
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.CALL_PHONE),
2333)
} else {
try {
val phoneAccountHandle = PhoneAccountHandle(ComponentName(
applicationContext,
MyConnectionService::class.java
), "ID999")
telecomManager.registerPhoneAccount(PhoneAccount.builder(
phoneAccountHandle,
"label"
).setCapabilities(PhoneAccount.CAPABILITY_CONNECTION_MANAGER) .build())
val extras = Bundle()
extras.putParcelable(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE, phoneAccountHandle)
telecomManager.placeCall(Uri.parse("tel:$phoneNumber"), extras)
} catch (e: SecurityException) {
e.printStackTrace()
}
}
Run Code Online (Sandbox Code Playgroud)
连接服务
class MyConnectionService : ConnectionService() {
private val TAG = "mycnnser"
override fun onCreate() {
super.onCreate()
Log.d(TAG, …Run Code Online (Sandbox Code Playgroud)