获取websocket数据后如何从单例对象拨打电话?

时间:2019-10-14 09:53:41

标签: android kotlin

我需要从object(Singleton)拨打电话。 该对象从互联网(websocket)获取一个电话号码,此后,我需要拨打电话(我的应用替换了默认的拨号程序)。 如何启动通话/活动/另一种开始通话的方式? 我已经尝试过Intent(Intent.ACTION_CALL, "some_number".toUri()),但这不起作用。

我使用this拨号程序示例作为地下室,只是添加了websocket(Scarlet)

我的对象

object Connection {

    fun connect() {

        val moshi = Moshi.Builder()
            .add(MoshiAdapters())
            .build()

        val okHttpClient = OkHttpClient.Builder()
            .readTimeout(0, TimeUnit.MILLISECONDS)
            .build()

        val protocol = OkHttpWebSocket(
            okHttpClient,
            OkHttpWebSocket.SimpleRequestFactory(
                { Request.Builder().url(wsUrl).build() },
                { ShutdownReason.GRACEFUL }
            )
        )

        val configuration = Scarlet.Configuration(
            messageAdapterFactories = listOf(MoshiMessageAdapter.Factory(moshi)),
            streamAdapterFactories = listOf(RxJava2StreamAdapterFactory())
        )

        val scarletInstance = Scarlet(protocol, configuration)

        val webSocketService = scarletInstance.create<WebSocketService>()

        webSocketService.observeStateTransition()
            .observeOn(Schedulers.io())
            .subscribe({ stateTransition ->
                val event = stateTransition.event
                val description = when (event) {
                    is Event.OnLifecycleStateChange -> when (event.lifecycleState) {
                        LifecycleState.Started -> Timber.d("On Lifecycle Start")
                        LifecycleState.Stopped -> Timber.d("On Lifecycle Stop")
                        LifecycleState.Completed -> Timber.d("On Lifecycle Terminate")
                    }
                    is Event.OnProtocolEvent -> {
                        when (stateTransition.toState) {
                            is State.WillConnect -> Timber.d("WaitingToRetry")
                            is State.Connecting -> Timber.d("Connecting")
                            is State.Connected -> Timber.d("Connected")
                            is State.Disconnecting -> Timber.d("Disconnecting")
                            is State.Disconnected -> Timber.d("Disconnected")
                            is State.Destroyed -> Timber.d("Destroyed")
                        }
                    }
                    Event.OnShouldConnect -> Timber.d("Should Connect")
                }
            }, { e ->
                Timber.e(e)
            })

        webSocketService.observeWebSocketEvent()
            .observeOn(Schedulers.io())
            .subscribe({ event ->
                val description = when (event) {
                    is WebSocketEvent.OnConnectionOpened -> {
                        Timber.d("On WebSocket Connection Opened")
                        Timber.d("Sent data")
                    }
                    is WebSocketEvent.OnMessageReceived -> {
                        Timber.d("On WebSocket NewMessageUpdate Received")
                    }
                    is WebSocketEvent.OnConnectionClosing -> {
                        Timber.d("On WebSocket Connection Closing")
                    }
                    is WebSocketEvent.OnConnectionClosed -> {
                        Timber.d("On WebSocket Connection Closed")
                    }
                    is WebSocketEvent.OnConnectionFailed -> {
                        Timber.d("On WebSocket Connection Failed")
                    }
                }
            },
                {
                    Timber.e(it)
                })



        webSocketService.observeIncomingMessage()
            .observeOn(Schedulers.io())
            .subscribe({
                 THERE I GET PHONE NUMBER AND AFTER SHOULD CALL
            })
    }}

在MainActivity中,我开始连接Connection.connect

我做错了什么,我应该怎么做?

0 个答案:

没有答案