简体   繁体   English

尝试戳我的Android并以编程方式打开屏幕

[英]Trying to Poke my Android and Turn on the Screen Programmatically

I am building an alarm kind of application for Android. 我正在为Android构建一种警报类型的应用程序。

I set a certain timer to run and then fire after a certain time interval (after the user presses a button) which I call in the onCreate method. 我将某个计时器设置为运行,然后在我在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);

I've also disabled the keyboard in the onCreate method with: 我还通过以下方式禁用了onCreate方法中的键盘:

getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD|
    WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);

In the Android developer docs, it says: 在Android开发人员文档中,它显示:

Window flag: when set as a window is being added or made visible, once the window has been shown then the system will poke the power manager's user activity (as if the user had woken up the device) to turn the screen on. 窗口标志:添加或设置为可见窗口后,一旦显示了该窗口,系统便会拨开电源管理器的用户活动(就像用户已经唤醒设备一样)以打开屏幕。

Now after I fire the timer with a press of the button, I immediately turn off the screen. 现在,按下按钮触发计时器后,我立即关闭屏幕。 After waiting 20 second though, the screen isn't poked and the device doesn't awaken. 但是,等待20秒后,屏幕没有戳破,设备也没有唤醒。 What could be the issue here? 这里可能是什么问题? When I do turn on the screen though with the power button after waiting another 20 seconds, I do find that the keyboard is disabled. 再等待20秒后,尽管确实使用电源按钮打开了屏幕,但我确实发现键盘已禁用。 Also the screen is perpetually awake when I do leave the screen on. 当我确实将屏幕保持打开状态时,屏幕也永远处于唤醒状态。 So I know the flags are working. 所以我知道这些标志在起作用。 I just need it to "wake" up when the timer fires! 我只需要它在计时器启动时“唤醒”!

you should use AlarmManager and PendingIntent 您应该使用AlarmManagerPendingIntent

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

Turns out, I had to properly group the right methods in the correct area. 事实证明,我不得不在正确的区域正确地组合正确的方法。 I had previously split the FLAG_DISMISS_KEYGUARD and FLAG_TURN_SCREEN_ON in different areas. 我之前曾将FLAG_DISMISS_KEYGUARD和FLAG_TURN_SCREEN_ON划分到不同的区域。

To successfully turn on the screen: 要成功打开屏幕,请执行以下操作:

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.

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