锁屏触发不同活动的应用程序生命周期事件

时间:2019-02-12 14:43:58

标签: android android-lifecycle lockscreen

当我在扩展FirebaseMessagingService的服务中收到推送消息并在此类中启动Activity时,将执行以下生命周期事件/方法:

  • 推送消息启动VideoRingingActivity
  • 启动VideoRingingActivity
  • onCreate
  • disableLockScreen
  • 屏幕从黑色切换到锁定屏幕, 因此它不会显示VideoRingingActivity
  • onStart
  • onResume
  • onPause
  • onStop
  • 现在显示了实际的VideoRingingActivity吗?我期待 这会在setContentView(R.layout.activity_video_ringing)之后出现
  • onRestart
  • onStart
  • onResume

我开发了以下变通办法,其中添加了一个布尔值,以确定屏幕是开还是关。但这很hacky,当屏幕在锁定模式下打开时,它将无法正常工作。

因为没有关闭屏幕并显示MainActivity并接收到推送消息,则该行为是默认的生命周期行为,因此是正确的。打开屏幕并显示MainActivity时,这是相同的流程:

  • onCreate
  • onStart
  • onResume

这是正确的,是可以预期的。

class VideoRingingActivity : BaseActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        if (ViewUtils.isScreenActive(activity)) {
            screenIsNotLocked = true
        }
        ViewUtils.disableLockScreen(this)
        binding = DataBindingUtil.setContentView(this, R.layout.activity_video_ringing)
    }

    override fun onStart() {
        super.onStart()
        if (screenIsNotLocked) {
        }
    }

    override fun onResume() {
        super.onResume()
        if (screenIsNotLocked) {
        }
    }

    override fun onPause() {
        super.onPause()
        if (screenIsNotLocked) {
        }
    }

    override fun onStop() {
        super.onStop()
        if (screenIsNotLocked) {
        }
        screenIsNotLocked = true
    }
}

open class ViewUtils {

    @JvmStatic
    @Suppress("DEPRECATION")
    fun disableLockScreen(activity: Activity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
            activity.setShowWhenLocked(true)
            activity.setTurnScreenOn(true)
        }
        val lock = (activity.getSystemService(Activity.KEYGUARD_SERVICE) as KeyguardManager).newKeyguardLock(Context.KEYGUARD_SERVICE)
        val powerManager = activity.getSystemService(Context.POWER_SERVICE) as PowerManager
        val wake = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK or PowerManager.ACQUIRE_CAUSES_WAKEUP or PowerManager.ON_AFTER_RELEASE, "BusSnoozeAlarm")

        lock.disableKeyguard()
        wake.acquire(TIMEOUT)

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

    @Suppress("DEPRECATION")
    fun isScreenActive(context: Context): Boolean {
        val powerManager = context.getSystemService(POWER_SERVICE) as PowerManager
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
            return powerManager.isInteractive
        } else {
            return powerManager.isScreenOn
        }
    }
}

更新:还有另一个SO线程也有类似的问题,但是解决方案已过时,并且无法解决在锁定屏幕模式下激活屏幕时的死角情况:

OnPause and OnStop() called immediately after starting activity

Why the onPause method is called immediately after onCreate

0 个答案:

没有答案