Android 如何使用Kotlin实现自我管理的ConnectionService

Android 如何使用Kotlin实现自我管理的ConnectionService,android,android-connectionservice,Android,Android Connectionservice,我是一个Android初学者,试图为我的Cordova应用程序设置Android代码 我的代码运行良好。没有任何错误。所有日志都会显示,但来电不会显示 我已将ConnectionService连接到FirebaseMessaging服务,以便在收到数据推送通知时触发传入呼叫 我收到通知,所有代码都成功运行。仍然没有来电界面。在Android 9和10设备上试用 MyConnectionService.kt @RequiresApi(Build.VERSION_CODES.M) class

我是一个Android初学者,试图为我的Cordova应用程序设置Android代码

我的代码运行良好。没有任何错误。所有日志都会显示,但来电不会显示

我已将ConnectionService连接到FirebaseMessaging服务,以便在收到数据推送通知时触发传入呼叫

我收到通知,所有代码都成功运行。仍然没有来电界面。在Android 9和10设备上试用

MyConnectionService.kt

    @RequiresApi(Build.VERSION_CODES.M)
class MyConnectionService : ConnectionService() {
    override fun onCreateOutgoingConnection(connectionManagerPhoneAccount: PhoneAccountHandle?, request: ConnectionRequest?): Connection {
    Log.i("CallConnectionService", "onCreateOutgoingConnection")
    val conn = VoipConnection(applicationContext)
    conn.setAddress(request!!.address, PRESENTATION_ALLOWED)
    conn.setInitializing()
    //conn.videoProvider = MyVideoProvider()
    conn.setActive()
    return conn
}

override fun onCreateIncomingConnection(connectionManagerPhoneAccount: PhoneAccountHandle?, request: ConnectionRequest?): Connection {
    Log.i("CallConnectionService", "onCreateIncomingConnection")
    val conn = VoipConnection(applicationContext)
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
        conn.connectionProperties = Connection.PROPERTY_SELF_MANAGED
    }
    conn.setCallerDisplayName("test call", TelecomManager.PRESENTATION_ALLOWED)
    //conn.setAddress(request!!.address, PRESENTATION_ALLOWED)
    conn.setRinging()
    conn.setInitializing()
    conn.setActive()
    return conn
}

override fun onCreateIncomingConnectionFailed(connectionManagerPhoneAccount: PhoneAccountHandle?, request: ConnectionRequest?) {
    super.onCreateIncomingConnectionFailed(connectionManagerPhoneAccount, request)
    Log.i("CallConnectionService", "create outgoing call failed ")
}

override fun onCreateOutgoingConnectionFailed(connectionManagerPhoneAccount: PhoneAccountHandle?, request: ConnectionRequest?) {
    super.onCreateOutgoingConnectionFailed(connectionManagerPhoneAccount, request)
    Log.i("CallConnectionService", "create outgoing call failed")
}
}
@RequiresApi(Build.VERSION_CODES.M)
class VoipConnection(ctx:Context) : Connection() {

    var ctx:Context = ctx
    val TAG = "CallConnection"

    override fun onShowIncomingCallUi() {
        Log.i(TAG, "onShowIncomingCallUi")
    }
class MyFirebaseMessagingService : FirebaseMessagingService() {

@RequiresApi(Build.VERSION_CODES.M)
override fun onMessageReceived(remoteMessage: RemoteMessage) {

    Log.d(TAG, "From: " + remoteMessage.from)

    try {
        val callManager = CallManager(this)
        val telecomManager = this.getSystemService(Context.TELECOM_SERVICE) as TelecomManager
        val componentName = ComponentName(this, MyConnectionService::class.java)
        val phoneAccountHandle = PhoneAccountHandle(componentName,"Admin")

        val callInfo = Bundle()
        callInfo.putString("from", "tester")
        telecomManager.addNewIncomingCall(phoneAccountHandle, callInfo)
    }
    catch (e: Exception) {
        Log.e("error", e.toString())
    }

}


override fun onNewToken(s: String) {
    Log.d(TAG, "Refreshed token: $s")
}

}
   @RequiresApi(Build.VERSION_CODES.M)
class CallManager(context: Context) {
    val telecomManager: TelecomManager
    var phoneAccountHandle: PhoneAccountHandle
    var context: Context
    val number = "3924823202"

init {
    telecomManager = context.getSystemService(Context.TELECOM_SERVICE) as TelecomManager
    this.context = context
    val componentName = ComponentName(this.context, MyConnectionService::class.java)
    phoneAccountHandle = PhoneAccountHandle(componentName, "Admin")
    val phoneAccount = PhoneAccount.builder(phoneAccountHandle, "Admin")
        .setCapabilities(PhoneAccount.CAPABILITY_SELF_MANAGED).build()


    telecomManager.registerPhoneAccount(phoneAccount)
    val intent = Intent()
    intent.component = ComponentName(
        "com.android.server.telecom",
        "com.android.server.telecom.settings.EnableAccountPreferenceActivity"
    )
    intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP
    Log.d("mainactivity", "init TelecomManager all well");

}

}
}
VoipConnection.kt

    @RequiresApi(Build.VERSION_CODES.M)
class MyConnectionService : ConnectionService() {
    override fun onCreateOutgoingConnection(connectionManagerPhoneAccount: PhoneAccountHandle?, request: ConnectionRequest?): Connection {
    Log.i("CallConnectionService", "onCreateOutgoingConnection")
    val conn = VoipConnection(applicationContext)
    conn.setAddress(request!!.address, PRESENTATION_ALLOWED)
    conn.setInitializing()
    //conn.videoProvider = MyVideoProvider()
    conn.setActive()
    return conn
}

override fun onCreateIncomingConnection(connectionManagerPhoneAccount: PhoneAccountHandle?, request: ConnectionRequest?): Connection {
    Log.i("CallConnectionService", "onCreateIncomingConnection")
    val conn = VoipConnection(applicationContext)
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
        conn.connectionProperties = Connection.PROPERTY_SELF_MANAGED
    }
    conn.setCallerDisplayName("test call", TelecomManager.PRESENTATION_ALLOWED)
    //conn.setAddress(request!!.address, PRESENTATION_ALLOWED)
    conn.setRinging()
    conn.setInitializing()
    conn.setActive()
    return conn
}

override fun onCreateIncomingConnectionFailed(connectionManagerPhoneAccount: PhoneAccountHandle?, request: ConnectionRequest?) {
    super.onCreateIncomingConnectionFailed(connectionManagerPhoneAccount, request)
    Log.i("CallConnectionService", "create outgoing call failed ")
}

override fun onCreateOutgoingConnectionFailed(connectionManagerPhoneAccount: PhoneAccountHandle?, request: ConnectionRequest?) {
    super.onCreateOutgoingConnectionFailed(connectionManagerPhoneAccount, request)
    Log.i("CallConnectionService", "create outgoing call failed")
}
}
@RequiresApi(Build.VERSION_CODES.M)
class VoipConnection(ctx:Context) : Connection() {

    var ctx:Context = ctx
    val TAG = "CallConnection"

    override fun onShowIncomingCallUi() {
        Log.i(TAG, "onShowIncomingCallUi")
    }
class MyFirebaseMessagingService : FirebaseMessagingService() {

@RequiresApi(Build.VERSION_CODES.M)
override fun onMessageReceived(remoteMessage: RemoteMessage) {

    Log.d(TAG, "From: " + remoteMessage.from)

    try {
        val callManager = CallManager(this)
        val telecomManager = this.getSystemService(Context.TELECOM_SERVICE) as TelecomManager
        val componentName = ComponentName(this, MyConnectionService::class.java)
        val phoneAccountHandle = PhoneAccountHandle(componentName,"Admin")

        val callInfo = Bundle()
        callInfo.putString("from", "tester")
        telecomManager.addNewIncomingCall(phoneAccountHandle, callInfo)
    }
    catch (e: Exception) {
        Log.e("error", e.toString())
    }

}


override fun onNewToken(s: String) {
    Log.d(TAG, "Refreshed token: $s")
}

}
   @RequiresApi(Build.VERSION_CODES.M)
class CallManager(context: Context) {
    val telecomManager: TelecomManager
    var phoneAccountHandle: PhoneAccountHandle
    var context: Context
    val number = "3924823202"

init {
    telecomManager = context.getSystemService(Context.TELECOM_SERVICE) as TelecomManager
    this.context = context
    val componentName = ComponentName(this.context, MyConnectionService::class.java)
    phoneAccountHandle = PhoneAccountHandle(componentName, "Admin")
    val phoneAccount = PhoneAccount.builder(phoneAccountHandle, "Admin")
        .setCapabilities(PhoneAccount.CAPABILITY_SELF_MANAGED).build()


    telecomManager.registerPhoneAccount(phoneAccount)
    val intent = Intent()
    intent.component = ComponentName(
        "com.android.server.telecom",
        "com.android.server.telecom.settings.EnableAccountPreferenceActivity"
    )
    intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP
    Log.d("mainactivity", "init TelecomManager all well");

}

}
}
FirebaseMessagingService.kt

    @RequiresApi(Build.VERSION_CODES.M)
class MyConnectionService : ConnectionService() {
    override fun onCreateOutgoingConnection(connectionManagerPhoneAccount: PhoneAccountHandle?, request: ConnectionRequest?): Connection {
    Log.i("CallConnectionService", "onCreateOutgoingConnection")
    val conn = VoipConnection(applicationContext)
    conn.setAddress(request!!.address, PRESENTATION_ALLOWED)
    conn.setInitializing()
    //conn.videoProvider = MyVideoProvider()
    conn.setActive()
    return conn
}

override fun onCreateIncomingConnection(connectionManagerPhoneAccount: PhoneAccountHandle?, request: ConnectionRequest?): Connection {
    Log.i("CallConnectionService", "onCreateIncomingConnection")
    val conn = VoipConnection(applicationContext)
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
        conn.connectionProperties = Connection.PROPERTY_SELF_MANAGED
    }
    conn.setCallerDisplayName("test call", TelecomManager.PRESENTATION_ALLOWED)
    //conn.setAddress(request!!.address, PRESENTATION_ALLOWED)
    conn.setRinging()
    conn.setInitializing()
    conn.setActive()
    return conn
}

override fun onCreateIncomingConnectionFailed(connectionManagerPhoneAccount: PhoneAccountHandle?, request: ConnectionRequest?) {
    super.onCreateIncomingConnectionFailed(connectionManagerPhoneAccount, request)
    Log.i("CallConnectionService", "create outgoing call failed ")
}

override fun onCreateOutgoingConnectionFailed(connectionManagerPhoneAccount: PhoneAccountHandle?, request: ConnectionRequest?) {
    super.onCreateOutgoingConnectionFailed(connectionManagerPhoneAccount, request)
    Log.i("CallConnectionService", "create outgoing call failed")
}
}
@RequiresApi(Build.VERSION_CODES.M)
class VoipConnection(ctx:Context) : Connection() {

    var ctx:Context = ctx
    val TAG = "CallConnection"

    override fun onShowIncomingCallUi() {
        Log.i(TAG, "onShowIncomingCallUi")
    }
class MyFirebaseMessagingService : FirebaseMessagingService() {

@RequiresApi(Build.VERSION_CODES.M)
override fun onMessageReceived(remoteMessage: RemoteMessage) {

    Log.d(TAG, "From: " + remoteMessage.from)

    try {
        val callManager = CallManager(this)
        val telecomManager = this.getSystemService(Context.TELECOM_SERVICE) as TelecomManager
        val componentName = ComponentName(this, MyConnectionService::class.java)
        val phoneAccountHandle = PhoneAccountHandle(componentName,"Admin")

        val callInfo = Bundle()
        callInfo.putString("from", "tester")
        telecomManager.addNewIncomingCall(phoneAccountHandle, callInfo)
    }
    catch (e: Exception) {
        Log.e("error", e.toString())
    }

}


override fun onNewToken(s: String) {
    Log.d(TAG, "Refreshed token: $s")
}

}
   @RequiresApi(Build.VERSION_CODES.M)
class CallManager(context: Context) {
    val telecomManager: TelecomManager
    var phoneAccountHandle: PhoneAccountHandle
    var context: Context
    val number = "3924823202"

init {
    telecomManager = context.getSystemService(Context.TELECOM_SERVICE) as TelecomManager
    this.context = context
    val componentName = ComponentName(this.context, MyConnectionService::class.java)
    phoneAccountHandle = PhoneAccountHandle(componentName, "Admin")
    val phoneAccount = PhoneAccount.builder(phoneAccountHandle, "Admin")
        .setCapabilities(PhoneAccount.CAPABILITY_SELF_MANAGED).build()


    telecomManager.registerPhoneAccount(phoneAccount)
    val intent = Intent()
    intent.component = ComponentName(
        "com.android.server.telecom",
        "com.android.server.telecom.settings.EnableAccountPreferenceActivity"
    )
    intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP
    Log.d("mainactivity", "init TelecomManager all well");

}

}
}
CallManger.kt

    @RequiresApi(Build.VERSION_CODES.M)
class MyConnectionService : ConnectionService() {
    override fun onCreateOutgoingConnection(connectionManagerPhoneAccount: PhoneAccountHandle?, request: ConnectionRequest?): Connection {
    Log.i("CallConnectionService", "onCreateOutgoingConnection")
    val conn = VoipConnection(applicationContext)
    conn.setAddress(request!!.address, PRESENTATION_ALLOWED)
    conn.setInitializing()
    //conn.videoProvider = MyVideoProvider()
    conn.setActive()
    return conn
}

override fun onCreateIncomingConnection(connectionManagerPhoneAccount: PhoneAccountHandle?, request: ConnectionRequest?): Connection {
    Log.i("CallConnectionService", "onCreateIncomingConnection")
    val conn = VoipConnection(applicationContext)
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
        conn.connectionProperties = Connection.PROPERTY_SELF_MANAGED
    }
    conn.setCallerDisplayName("test call", TelecomManager.PRESENTATION_ALLOWED)
    //conn.setAddress(request!!.address, PRESENTATION_ALLOWED)
    conn.setRinging()
    conn.setInitializing()
    conn.setActive()
    return conn
}

override fun onCreateIncomingConnectionFailed(connectionManagerPhoneAccount: PhoneAccountHandle?, request: ConnectionRequest?) {
    super.onCreateIncomingConnectionFailed(connectionManagerPhoneAccount, request)
    Log.i("CallConnectionService", "create outgoing call failed ")
}

override fun onCreateOutgoingConnectionFailed(connectionManagerPhoneAccount: PhoneAccountHandle?, request: ConnectionRequest?) {
    super.onCreateOutgoingConnectionFailed(connectionManagerPhoneAccount, request)
    Log.i("CallConnectionService", "create outgoing call failed")
}
}
@RequiresApi(Build.VERSION_CODES.M)
class VoipConnection(ctx:Context) : Connection() {

    var ctx:Context = ctx
    val TAG = "CallConnection"

    override fun onShowIncomingCallUi() {
        Log.i(TAG, "onShowIncomingCallUi")
    }
class MyFirebaseMessagingService : FirebaseMessagingService() {

@RequiresApi(Build.VERSION_CODES.M)
override fun onMessageReceived(remoteMessage: RemoteMessage) {

    Log.d(TAG, "From: " + remoteMessage.from)

    try {
        val callManager = CallManager(this)
        val telecomManager = this.getSystemService(Context.TELECOM_SERVICE) as TelecomManager
        val componentName = ComponentName(this, MyConnectionService::class.java)
        val phoneAccountHandle = PhoneAccountHandle(componentName,"Admin")

        val callInfo = Bundle()
        callInfo.putString("from", "tester")
        telecomManager.addNewIncomingCall(phoneAccountHandle, callInfo)
    }
    catch (e: Exception) {
        Log.e("error", e.toString())
    }

}


override fun onNewToken(s: String) {
    Log.d(TAG, "Refreshed token: $s")
}

}
   @RequiresApi(Build.VERSION_CODES.M)
class CallManager(context: Context) {
    val telecomManager: TelecomManager
    var phoneAccountHandle: PhoneAccountHandle
    var context: Context
    val number = "3924823202"

init {
    telecomManager = context.getSystemService(Context.TELECOM_SERVICE) as TelecomManager
    this.context = context
    val componentName = ComponentName(this.context, MyConnectionService::class.java)
    phoneAccountHandle = PhoneAccountHandle(componentName, "Admin")
    val phoneAccount = PhoneAccount.builder(phoneAccountHandle, "Admin")
        .setCapabilities(PhoneAccount.CAPABILITY_SELF_MANAGED).build()


    telecomManager.registerPhoneAccount(phoneAccount)
    val intent = Intent()
    intent.component = ComponentName(
        "com.android.server.telecom",
        "com.android.server.telecom.settings.EnableAccountPreferenceActivity"
    )
    intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP
    Log.d("mainactivity", "init TelecomManager all well");

}

}
}