How can i listen a incoming video call from my app

2020-05-03 09:47发布

问题:

i need to show call accept/reject screen while a video call coming in my app using OpenTok SDK. I should also listen incoming call while my app is killed. What can i do for listen video call from app. their is any way to listen and when i reject or skip the call then it will show in my call history as missed call. Thanks in Advance:)

回答1:

Create a video call coming VideoCallComingActivity activity

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        // setContentView(you layout id)

        // Setup your activity which can open when the screen is clocked
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
            setShowWhenLocked(true)
            setTurnScreenOn(true)
            (getSystemService(Context.KEYGUARD_SERVICE) as KeyguardManager).requestDismissKeyguard(this, null)
        } else {
            window?.addFlags(
                    WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD or
                            WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED or
                            WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
            )
        } 
}

Using JobIntentService service for starting the VideoCallComingActivity activity. Using JobIntentService to avoid the background restriction from Android 8 or higher. Create YourJobIntentService extends from JobIntentService

class YourJobIntentService : JobIntentService() {
    override fun onHandleWork(it: Intent) {
        // Start the VideoCallComingActivity activity
    }

    // OR static method in Java
    companion object {
        fun enqueueWork(context: Context, intent: Intent) {
            enqueueWork(context, YourJobIntentService::class.java, 1000, intent)
        }
    }
}

Using Firebase to receiving notification about a video call coming:

    override fun onMessageReceived(remoteMessage: RemoteMessage?) {
        super.onMessageReceived(remoteMessage)

        // More code here
        YourJobIntentService.enqueueWork(applicationContext, intent)
    }

when i reject or skip the call then it will show in my call history as missed call -> I think it depends on your app logic.