关于AlarmManager和WakeLock

伯洋
2023-12-01
1.AlarmManager要想在休眠时也起作用,需要使用RTC_WAKEUP类型
2.休眠时,AlarmManager设置的PendingIntent要直接start一个Activity,往往无效,
此时必需先sendBroadcast,在broadcast里面获取一个timeout的wakeLock,再startActivity,才能成功
3.aquireWakeLock要想主动亮屏,需要在创建时设置PowerManager.FULL_WAKE_LOCK|PowerManager.ACQUIRE_CAUSES_WAKEUP参数
4.在某界面想要不锁屏,需要对窗口设置以下属性
    private void setUnlocked()
    {
        Window win = getWindow();
        WindowManager.LayoutParams winParams = win.getAttributes();
        winParams.flags |= (WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
                | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                | WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON
                | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
        win.setAttributes(winParams);
    }

5.wakeLock为防止出现wakelock under-locked的错误,可以设置setReferenceCounted(false);

6.定时任务Timer, TimerTask,Handler, Runnable, runOnUiThread,AlarmManager各有特点


 类似资料: