即使屏幕关闭也开始活动 [英] Start activity even when screen is turned off

查看:28
本文介绍了即使屏幕关闭也开始活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过闹钟开始一项活动.PendingIntent 启动接收器,接收器启动活动.我目前的问题是活动在后台开始,无法听到警报声.Oreo 和较新的设备不推荐使用旧 SO 问题中的大多数标志.有没有人有一个很好的方法来处理这个问题?

I'm trying to start an activity with an alarm. The PendingIntent starts a receiver and the receiver starts the activity. 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. Does anyone have a good approach how to handle this?

提前致谢

警报创建:

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

接收器

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

活动:

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()
    }
}

推荐答案

你应该在你的 AndroidManifest.xml 中有

You should have in your AndroidManifest.xml

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

此外,以下检查应该在 setContentView() 之后.因为在您添加标志时,还没有可以使用它们的视图.

Also the following check should be after the 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)
}

这篇关于即使屏幕关闭也开始活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆