Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
397 views
in Technique[技术] by (71.8m points)

android - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...