【问题标题】:How to show the incoming call screen when the screen is locked?屏幕锁定时如何显示来电屏幕?
【发布时间】:2018-11-07 14:52:05
【问题描述】:

我正在开发我的自定义呼叫应用程序,例如 Skype,当我收到 fcm 消息时,我需要向用户显示“来电”屏幕。为此,我使用全屏意图通知。我现在的代码是这样的:

        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)

当屏幕解锁时,我会收到带有按钮的呼叫通知,用于接听和拒绝。但是当屏幕被锁定时,我只收到声音和振动,而不是带有电报、whatsup 和 viber 等按钮的自定义屏幕。设备锁定时如何显示这样的自定义屏幕?

【问题讨论】:

    标签: android webrtc voip


    【解决方案1】:

    在您的 CallActivity 中添加以下代码:

    getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON|WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
    

    【讨论】:

    • Aolphn,我可以添加一些活动按钮或从我的 CallActivity 开始新活动吗?如何在此处添加接受和拒绝按钮?
    • 添加按钮?就像正常活动一样,它会起作用,试试吧,如果你有任何问题,请告诉我。
    • 是的,问题是我无法在此活动中加载 webview。好像太重了。但所有按钮都在工作。谢谢。
    • 伙计们,我不明白把这段代码放在哪里?在接收器活动或其他地方?我有一些问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多