从服务中打开屏幕和停止屏幕保护程序

时间:2018-04-10 00:40:55

标签: android

我有一项服务,应该唤醒设备并在收到意图时停止屏幕保护程序。任何想法如何实现?

在活动中,我通常会这样做:

getWindow().addFlags(android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

getWindow().addFlags(android.view.WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);

问题是我没有Window在服务中。

1 个答案:

答案 0 :(得分:0)

在接收器中

IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
intentFilter.addAction(Intent.ACTION_SCREEN_ON);

    BroadcastReceiver screenOnOff = new BroadcastReceiver()
    {
        public static final String ScreenOff = "android.intent.action.SCREEN_OFF";
        public static final String ScreenOn = "android.intent.action.SCREEN_ON";

        public void onReceive(Context contex, Intent intent)
        {
            if (intent.getAction().equals(ScreenOff))
            {
                Log.e("Log", "Screen Off"); 
            }
            else if (intent.getAction().equals(ScreenOn))
            {
                Log.e("Log", "Screen On"); 
            }
        }
    };