[英]Trying to Poke my Android and Turn on the Screen Programmatically
我正在為Android構建一種警報類型的應用程序。
我將某個計時器設置為運行,然后在我在onCreate方法中調用的特定時間間隔(用戶按下按鈕之后)后觸發。
final int interval = 20000; // 20 Second
Handler handler = new Handler();
Runnable runnable = new Runnable(){
public void run() {
Toast.makeText(getApplicationContext(), "Timer to Wake",Toast.LENGTH_SHORT).show();
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON|WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
};
handler.postAtTime(runnable, System.currentTimeMillis()+interval);
handler.postDelayed(runnable, interval);
我還通過以下方式禁用了onCreate方法中的鍵盤:
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD|
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
在Android開發人員文檔中,它顯示:
窗口標志:添加或設置為可見窗口后,一旦顯示了該窗口,系統便會撥開電源管理器的用戶活動(就像用戶已經喚醒設備一樣)以打開屏幕。
現在,按下按鈕觸發計時器后,我立即關閉屏幕。 但是,等待20秒后,屏幕沒有戳破,設備也沒有喚醒。 這里可能是什么問題? 再等待20秒后,盡管確實使用電源按鈕打開了屏幕,但我確實發現鍵盤已禁用。 當我確實將屏幕保持打開狀態時,屏幕也永遠處於喚醒狀態。 所以我知道這些標志在起作用。 我只需要它在計時器啟動時“喚醒”!
您應該使用AlarmManager
和PendingIntent
AlarmManager am=(AlarmManager)getApplicationContext getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, SampleActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),(9 * 1000), pendingIntent);
事實證明,我不得不在正確的區域正確地組合正確的方法。 我之前曾將FLAG_DISMISS_KEYGUARD和FLAG_TURN_SCREEN_ON划分到不同的區域。
要成功打開屏幕,請執行以下操作:
public void run() {
Toast.makeText(getApplicationContext(), "Timer to Wake", Toast.LENGTH_SHORT).show();
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.