1

我需要拦截设备框架发出的呼出事件。
按照android 指南,我停在第 3 点Telecom 子系统绑定到您的应用的 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()
        }
    }

连接服务

class MyConnectionService : ConnectionService() {
private val TAG = "mycnnser"
override fun onCreate() {
    super.onCreate()
    Log.d(TAG, "onCreate: ")
}

override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
    Log.d(TAG, "onStartCommand: ")
    return super.onStartCommand(intent, flags, startId)
}

override fun onCreateIncomingConnection(connectionManagerPhoneAccount: PhoneAccountHandle, request: ConnectionRequest): Connection {
    Log.d(TAG, "onCreateIncomingConnection: ")
    return super.onCreateIncomingConnection(connectionManagerPhoneAccount, request)
}

override fun onCreateIncomingConnectionFailed(connectionManagerPhoneAccount: PhoneAccountHandle, request: ConnectionRequest) {
    Log.d(TAG, "onCreateIncomingConnectionFailed: ")
    super.onCreateIncomingConnectionFailed(connectionManagerPhoneAccount, request)
}

override fun onCreateOutgoingConnectionFailed(connectionManagerPhoneAccount: PhoneAccountHandle, request: ConnectionRequest) {
    Log.d(TAG, "onCreateOutgoingConnectionFailed: ")
    super.onCreateOutgoingConnectionFailed(connectionManagerPhoneAccount, request)
}

override fun onCreateOutgoingConnection(connectionManagerPhoneAccount: PhoneAccountHandle, request: ConnectionRequest): Connection {
    Log.d(TAG, "onCreateOutgoingConnection: ")
    return super.onCreateOutgoingConnection(connectionManagerPhoneAccount, request)
}

override fun onCreateOutgoingHandoverConnection(fromPhoneAccountHandle: PhoneAccountHandle, request: ConnectionRequest): Connection {
    Log.d(TAG, "onCreateOutgoingHandoverConnection: ")
    return super.onCreateOutgoingHandoverConnection(fromPhoneAccountHandle, request)
}

override fun onCreateIncomingHandoverConnection(fromPhoneAccountHandle: PhoneAccountHandle, request: ConnectionRequest): Connection {
    Log.d(TAG, "onCreateIncomingHandoverConnection: ")
    return super.onCreateIncomingHandoverConnection(fromPhoneAccountHandle, request)
}

override fun onHandoverFailed(request: ConnectionRequest, error: Int) {
    super.onHandoverFailed(request, error)
    Log.d(TAG, "onHandoverFailed: ")
}

override fun onConference(connection1: Connection, connection2: Connection) {
    super.onConference(connection1, connection2)
    Log.d(TAG, "onConference: ")
}

override fun onRemoteConferenceAdded(conference: RemoteConference) {
    super.onRemoteConferenceAdded(conference)
    Log.d(TAG, "onRemoteConferenceAdded: ")
}

override fun onRemoteExistingConnectionAdded(connection: RemoteConnection) {
    super.onRemoteExistingConnectionAdded(connection)
    Log.d(TAG, "onRemoteExistingConnectionAdded: ")
}

override fun onConnectionServiceFocusLost() {
    super.onConnectionServiceFocusLost()
    Log.d(TAG, "onConnectionServiceFocusLost: ")
}

override fun onConnectionServiceFocusGained() {
    super.onConnectionServiceFocusGained()
    Log.d(TAG, "onConnectionServiceFocusGained: ")
}}

显现

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.thorny.myapplication">

  <uses-permission android:name="android.permission.MANAGE_OWN_CALLS"/>
  <uses-permission android:name="android.permission.READ_CALL_LOG"/>
  <uses-permission android:name="android.permission.READ_PHONE_STATE"
      android:maxSdkVersion="29"/>
  <uses-permission android:name="android.permissions.READ_PHONE_NUMBERS"/>
  <uses-permission
      android:name="android.permission.CALL_PHONE" />

  <application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/Theme.MyApplication">
    <activity android:name=".MainActivity">
      <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
    </activity>
    <service android:name=".MyConnectionService"
        android:permission="android.permission.BIND_TELECOM_CONNECTION_SERVICE">
      <intent-filter>
        <action android:name="android.telecom.ConnectionService" />
      </intent-filter>
    </service>
  </application>

</manifest>

问题:调用通过设备框架开始,但从未触发所有服务日志。我的 Android 版本是 10。谢谢

4

0 回答 0