Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show the incoming call screen when the screen is locked?

I'm developing my custom calling app, like skype and I need to show "incoming call" screen to user, when I receive fcm message. I use full screen intent notification for this purpose. My code now is like this:

        val intent = Intent(Intent.ACTION_MAIN, null)
        val fakeIntent = Intent()
        intent.flags = Intent.FLAG_ACTIVITY_NO_USER_ACTION or Intent.FLAG_ACTIVITY_NEW_TASK
        intent.setClass(ctx, IncomingCallActivity::class.java!!)
        val pendingIntent = PendingIntent.getActivity(ctx, 1, intent, 0)
        val pendingIntent2 = PendingIntent.getActivity(ctx, 1, fakeIntent, PendingIntent.FLAG_ONE_SHOT)
        val builder = Notification.Builder(ctx)
        builder.setOngoing(true)
        builder.setPriority(Notification.PRIORITY_HIGH)

        // Set notification content intent to take user to fullscreen UI if user taps on the
        // notification body.
        builder.setContentIntent(pendingIntent)
        // Set full screen intent to trigger display of the fullscreen UI when the notification
        // manager deems it appropriate.
        builder.setFullScreenIntent(pendingIntent, true)

        // Setup notification content.
        builder.setSmallIcon(R.mipmap.ic_launcher)
        builder.setContentTitle("Call from Vitalii")
        builder.setContentText("Your notification content.")
        builder.setAutoCancel(true)


        // Use builder.addAction(..) to add buttons to answer or reject the call.
        val acceptAction = Notification.Action.Builder(Icon.createWithResource(ctx, R.drawable.ic_launch), "Accept", pendingIntent).build()
        val declineAction = Notification.Action.Builder(Icon.createWithResource(ctx, R.drawable.ic_launch), "Decline", pendingIntent2).build()
        builder.addAction(acceptAction)
        builder.addAction(declineAction)
        val notificationManager = ctx.getSystemService(
                NotificationManager::class.java)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val channel = NotificationChannel("callnotification", "Incoming Calls", NotificationManager.IMPORTANCE_MAX)
            val ringtoneUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
            channel.setSound(ringtoneUri, AudioAttributes.Builder()
                    .setUsage(AudioAttributes.USAGE_NOTIFICATION_RINGTONE)
                    .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                    .build())
            notificationManager.createNotificationChannel(channel)
            builder.setChannelId("callnotification")
        }
        val notification = builder.build()
        notification.flags = Notification.FLAG_INSISTENT
        currentNotificationId = Random().nextInt(500)
        intent.putExtra("notifid", currentNotificationId)
        notificationManager.notify("callnotification", currentNotificationId, notification)

And when screen is unlocked I receive calling notification with buttons to accept the call and decline. But when screen is locked I receive only sound and vibration, but not the custom screen with buttons like in telegram, whatsup and viber. How can I show such custom screen when device is locked?

like image 758
WorieN Avatar asked Nov 07 '18 14:11

WorieN


People also ask

Why my incoming calls are not displaying on screen?

Step 1: Go to App Info of the Phone Dialer app and tap on Notifications. Step 2: Tap on the 'Incoming Calls' option and then on behavior. Step 3: Now tap on 'Behaviour'. Step 4: Ensure that the notification priority is set to urgent or “Make sound and pop up”.


1 Answers

Add following code in your CallActivity:

getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON|WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
like image 152
aolphn Avatar answered Oct 28 '22 23:10

aolphn