简体   繁体   English

即使关闭屏幕也可以开始活动

[英]Start activity even when screen is turned off

I'm trying to start an activity with an alarm. 我正在尝试通过警报启动活动。 The PendingIntent starts a receiver and the receiver starts the activity. PendingIntent启动接收器,接收器启动活动。 My current issue is that the activity starts in the background and it's impossible to hear the alarm sound. 我当前的问题是活动从后台开始,无法听到警报声。 Most of the flags from older SO questions are deprecated for Oreo and newer devices. 较旧的SO问题中的大多数标志对于Oreo和较新的设备均已弃用。 Does anyone have a good approach how to handle this? 有谁有一个好的方法来处理这个问题?

Thank you in advance 先感谢您

Alarm creation: 警报创建:

alarmManager.setExact(AlarmManager.RTC_WAKEUP, intervalFinished, pendingIntent)

Receiver 接收器

class OnAlarmReceiver : BroadcastReceiver() {

    override fun onReceive(context: Context, intent: Intent) {
        val intent = Intent(context, AlarmActivity::class.java)
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)

        context.startActivity(intent)
    }
}

Activity: 活动:

private var wake: PowerManager.WakeLock? = null

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    val powerManager = getSystemService(Context.POWER_SERVICE) as PowerManager
    wake = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK
            or PowerManager.ACQUIRE_CAUSES_WAKEUP, "App:wakeuptag")
    wake?.acquire(10*60*1000L /*10 minutes*/)

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
        setShowWhenLocked(true)
        setTurnScreenOn(true)
    } else {
        window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                or WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
                or WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
                or WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
                or WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON)
    }
    setContentView(R.layout.activity_layout)
}

override fun onPause() {
    super.onPause()
    if(wake != null && wake!!.isHeld){
        wake!!.release()
    }
}

You should have in your AndroidManifest.xml 您应该在AndroidManifest.xml中

<activity
    android:name=".AlarmActivity"
    android:showOnLockScreen="true"
    android:turnScreenOn="true"/>

Also the following check should be after the setContentView() . 同样,以下检查也应在setContentView()之后 Since at the time you adding the flags there is not view that can make use of them. 由于在添加标志时,没有可以使用它们的视图。

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
    setShowWhenLocked(true)
    setTurnScreenOn(true)
} else {
    window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
           or WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
           or WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
           or WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
           or WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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