简体   繁体   中英

Call service opens the app automatically from background

I have a call service which opens the call screen whenever a incoming call is received, the problem is if my app is in background and if there is a call and when the call is over, the app automatically opens from background activity. If the app is not in background and a call is received and when it is over the app doesn't open ups.

Is this a default behavior or I am doing something wrong.

I don't want the app to automatically launch from background.

private val callReceiver = object : BroadcastReceiver() {
        override fun onReceive(context: Context, intent: Intent?) {
            val callId = intent?.getStringExtra(KEY_CALL_ID)
            val isVideoCall = intent?.getBooleanExtra(KEY_IS_VIDEO, false) ?: false
            callId?.let {
                RtcCallActivity.startIncomingCall(context, callId, isVideoCall)
            }
        }
    }

fun startIncomingCall(context: Context, callId: String?, videoEnabled: Boolean) {
            val intent = Intent(context, CallActivity::class.java).apply {
                addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                putExtra(CallService.VIDEO_ENABLED, videoEnabled)
                putExtra(INCOMING_CALL, true)
                putExtra(CALL_ID, callId)
            }
            context.startActivity(intent)
        }

We are implementing call feature like whatsapp->

  1. Incoming call (app could be in background)
  2. Call is over
  3. Now the phone should be in previous state (The whatsapp will not be open up after the call is over)

No solution found, now I am handling like this->

First I am checking if the app was in background, took help from this function->

fun isApplicationForeground(context: Context): Boolean {
    val keyguardManager = context.getSystemService(Context.KEYGUARD_SERVICE) as KeyguardManager
    if (keyguardManager.isKeyguardLocked) {
        return false
    }
    val myPid = Process.myPid()
    val activityManager = context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
    var list: List<ActivityManager.RunningAppProcessInfo>
    if (activityManager.runningAppProcesses.also { list = it } != null) {
        for (aList in list) {
            var info: ActivityManager.RunningAppProcessInfo
            if (aList.also { info = it }.pid == myPid) {
                return info.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND
            }
        }
    }
    return false
}

Then finally when the call is ended I am calling moveTaskToBack(true) ->

fun onCallEnded() {
    if (isApplicationForeground(context)==false) {
        moveTaskToBack(true)
    }
    finishAndRemoveTask()
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM