【问题标题】:Full screen intent doesn't display on lock screen全屏意图不显示在锁定屏幕上
【发布时间】:2022-01-10 12:41:32
【问题描述】:

所以在我的应用程序中,当时间到了时,我想启动一个活动来通知用户,然后让他们解除警报。

我尝试通过安排一个确切的警报然后从我的 AlarmReceiver 的onReceive() 启动具有全屏意图的高优先级通知来实现它。问题是屏幕锁定时活动不会启动,我收到的只是一个提示通知,它甚至不会打开屏幕,也不会振动。它没有在我的手机(带有 Android 7.1.2 的小米 X4)上启动,但它在我尝试过的另一部手机(带有 Android 6 的三星 Galaxy A5)上启动。我知道这可以在我的手机上实现,因为我观察到时钟、电话、whatsapp 等其他应用程序可以做到这一点。

Androidmanifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.app">

    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
    <uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
    <uses-permission android:name="android.permission.USE_FULL_SCREEN_INTENT" />
    <uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.App">
        <activity
            android:taskAffinity=""
            android:launchMode="singleInstance"
            android:showForAllUsers="true"
            android:excludeFromRecents="true"
            android:name=".TimeIsUpActivity" />


        <activity
            android:name=".MainActivity"
            android:exported="true"
            android:showOnLockScreen="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver
            android:name=".AlarmReceiver"
            android:enabled="true" />
    </application>

</manifest>

这是我设置闹钟的方式:

        val alarmMgr = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
        alarmIntent = Intent(context, AlarmReceiver::class.java).let { intent ->
            PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
        }

        val seconds = 5

        alarmMgr.setExactAndAllowWhileIdle(
            AlarmManager.ELAPSED_REALTIME_WAKEUP,
            SystemClock.elapsedRealtime() + seconds*1000,
            alarmIntent
        )

我的闹钟接收器:

class AlarmReceiver: BroadcastReceiver(){
    override fun onReceive(context: Context?, intent: Intent?) {
        context?.apply {
            val fullScreenIntent = Intent(this, TimeIsUpActivity::class.java)
            fullScreenIntent.flags =
                Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK or
                    Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS or Intent.FLAG_ACTIVITY_NO_USER_ACTION

            val fullScreenPendingIntent = PendingIntent.getActivity(this, 0,
                fullScreenIntent, PendingIntent.FLAG_UPDATE_CURRENT)

            val builder = NotificationCompat.Builder(this, getString(R.string.channel_id))
                .setContentTitle("Time is up")
                .setContentText("Tap to dismiss")
                .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
                .setContentIntent(fullScreenPendingIntent)
                .setSmallIcon(R.drawable.ic_add)
                .setVibrate(longArrayOf(1000, 1000, 1000, 1000, 1000))
                .setOngoing(true)
                .setLights(0xFFFFFF, 1000, 1000)
                .setCategory(NotificationCompat.CATEGORY_ALARM)
                .setPriority(NotificationCompat.PRIORITY_MAX)
                .setFullScreenIntent(fullScreenPendingIntent, true)


            val notificationId = Random().nextInt()
            val notification = builder.build()

            with(NotificationManagerCompat.from(this)) {
                notify(notificationId, notification)
            }
        }
    }
}

我要发起的活动:

class TimeIsUpActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        turnScreenOnAndKeyguardOff()
        setContentView(R.layout.activity_time_is_up)
    }

    private fun turnScreenOnAndKeyguardOff() {
        if (Build.VERSION.SDK_INT >= 27) {
            setShowWhenLocked(true)
            setTurnScreenOn(true)
            (getSystemService(Context.KEYGUARD_SERVICE) as KeyguardManager).also {
                it.requestDismissKeyguard(this, null)
            }
        }

        window.addFlags(
            WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON or
                    WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON or
                    WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED or
                    WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON)
    }
}

请帮帮我。

【问题讨论】:

    标签: android android-intent android-activity android-notifications alarmmanager


    【解决方案1】:

    我发现这是因为小米设备默认不允许应用在锁屏上显示。要允许它,必须转到设置 -> 权限 -> 其他权限 -> 找到您的应用程序 -> 选中“在锁定屏幕上显示”。在较新的设备上可能会有所不同。

    【讨论】:

      【解决方案2】:

      我想你是因为Appear on top。如果您的应用程序无权显示在顶部,则必须询问用户。所以你必须添加这样的东西:

          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
              val requestIntent = Intent(
                  Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                  Uri.parse("package:${activity.packageName}")
              )
              startActivityForResult(requestIntent, REQUEST_APPEAR_ON_TOP)
              
          }
      

      【讨论】:

        猜你喜欢
        • 2020-06-17
        • 1970-01-01
        • 2023-04-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多