唤醒 Android 设备

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3621599/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:02:05  来源:igfitidea点击:

Wake Android Device up

androidwakeup

提问by Coxer

Hey i need to wake my sleeping android device up at a certain time. Any suggestions?

嘿,我需要在特定时间唤醒我沉睡的 android 设备。有什么建议?

P.S. Wake up: turn display on and maybe unlock phone

PS 唤醒:打开显示并可能解锁手机

采纳答案by hackbod

Best is to use some appropriate combination of these window flags:

最好是使用这些窗口标志的一些适当组合:

http://developer.android.com/reference/android/view/WindowManager.LayoutParams.html#FLAG_DISMISS_KEYGUARD
http://developer.android.com/reference/android/view/WindowManager.LayoutParams.html#FLAG_SHOW_WHEN_LOCKED
http://developer.android.com/reference/android/view/WindowManager.LayoutParams.html#FLAG_KEEP_SCREEN_ON
http://developer.android.com/reference/android/view/WindowManager.LayoutParams.html#FLAG_TURN_SCREEN_ON

http://developer.android.com/reference/android/view/WindowManager.LayoutParams.html#FLAG_DISMISS_KEYGUARD
http://developer.android.com/reference/android/view/WindowManager.LayoutParams.html#FLAG_SHOW_WHEN_LOCKED
http:// developer.android.com/reference/android/view/WindowManager.LayoutParams.html#FLAG_KEEP_SCREEN_ON
http://developer.android.com/reference/android/view/WindowManager.LayoutParams.html#FLAG_TURN_SCREEN_ON

If you want to run on older versions of the platform that don't support the desired flag(s), you can directly use wake locks and keyguard locks... but that path is fraught with peril.

如果您想在不支持所需标志的旧版本平台上运行,您可以直接使用唤醒锁和键盘锁……但这条路充满了危险。

ONE IMPORTANT NOTE: Your activity must be full screenin order for the above flag combination to work. In my app I tried to use these flags with an activity which is not full screen (Dialog Theme) and it didn't work. After looking at the documentation I found that these flags require the window to be a full screen window.

一个重要注意事项:您的活动必须全屏显示才能使上述标志组合起作用。在我的应用程序中,我尝试将这些标志用于非全屏(对话框主题)的活动,但它不起作用。查看文档后,我发现这些标志要求窗口是全屏窗口。

回答by Yar

To wake up the screen:

唤醒屏幕:

PowerManager pm = (PowerManager) getApplicationContext().getSystemService(Context.POWER_SERVICE);
WakeLock wakeLock = pm.newWakeLock((PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP), "TAG");
wakeLock.acquire();

To release the screen lock:

要解除屏幕锁定:

KeyguardManager keyguardManager = (KeyguardManager) getApplicationContext().getSystemService(Context.KEYGUARD_SERVICE); 
KeyguardLock keyguardLock = keyguardManager.newKeyguardLock("TAG");
keyguardLock.disableKeyguard();

And the manifest needs to contain:

清单需要包含:

<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />

For more details about PowerManager, refer to the API documentation: http://developer.android.com/reference/android/os/PowerManager.html

有关 PowerManager 的更多详细信息,请参阅 API 文档:http: //developer.android.com/reference/android/os/PowerManager.html

EDIT: this answer is reported as deprecated.

编辑:这个答案被报告为已弃用。

回答by mishkin

I found a way and it is not that complex... works on any API version.

我找到了一种方法,它并不复杂……适用于任何 API 版本。

You need to use PowerManager.userActivity(l, false)method and register your activity as broadcast received for SCREEN_OFF intent:

您需要使用PowerManager.userActivity(l, false)方法并将您的活动注册为 SCREEN_OFF 意图接收到的广播:

In your actiivity OnCreate put something like:

在您的活动 OnCreate 中放置如下内容:

mReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.v(TAG, "Screen OFF onReceive()");
        screenOFFHandler.sendEmptyMessageDelayed(0, 2000L);
    }
};

It will kick off the handler after 2 seconds of Screen Off event.

它将在 Screen Off 事件发生 2 秒后启动处理程序。

Register receiver in your onResume() method:

在您的 onResume() 方法中注册接收器:

IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
registerReceiver(mReceiver, filter);
Log.i(TAG, "broadcast receiver registered!");

Create a handler like the one below:

创建一个如下所示的处理程序:

private Handler screenOFFHandler = new Handler() {

    @Override
    public void handleMessage(Message msg) {

        super.handleMessage(msg);
        // do something
        // wake up phone
        Log.i(TAG, "ake up the phone and disable keyguard");
        PowerManager powerManager = (PowerManager) YourActivityName.this
                .getSystemService(Context.POWER_SERVICE);
        long l = SystemClock.uptimeMillis();
        powerManager.userActivity(l, false);//false will bring the screen back as bright as it was, true - will dim it
    }
};

Request permission in your manifest file:

在清单文件中请求权限:

<uses-permission android:name="android.permission.WAKE_LOCK" />

Do not forget to unregister broadcast receiver when you are done. You may do that in onDestroy() for example (which is not guaranteed)

完成后不要忘记注销广播接收器。例如,您可以在 onDestroy() 中执行此操作(不能保证)

unregisterReceiver(mReceiver);
Log.i(TAG, "broadcast UNregistred!");

回答by Lahiru Chandima

If you are showing a window when waking up, you can get it working easily by adding few flags to your activity, without using a wake lock.

如果您在醒来时显示一个窗口,您可以通过向您的活动添加几个标志来轻松使其工作,而无需使用唤醒锁。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.my_activity);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
}

回答by Palm

On newer devices you should use something like this, since the mentioned Flags are deprecated.

在较新的设备上你应该使用这样的东西,因为提到的标志已被弃用。

class AlarmActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {

        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_alarm)

        // Keep screen always on, unless the user interacts (wakes the mess up...)
        window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)

        setTurnScreenOn(true)
        setShowWhenLocked(true)

        (getSystemService(KeyguardManager::class.java) as KeyguardManager).requestDismissKeyguard(this,
            object: KeyguardManager.KeyguardDismissCallback(){
                override fun onDismissCancelled() {
                    Log.d("Keyguard", "Cancelled")
                }

                override fun onDismissError() {
                    Log.d("Keyguard", "Error")
                }

                override fun onDismissSucceeded() {
                    Log.d("Keyguard", "Success")
                }
            }
        )
    }
}

KeyguardManager.requestDismissKeyguardonly wakes up the device, if the setter setTurnScreenOn(true)was called before.

KeyguardManager.requestDismissKeyguard如果setTurnScreenOn(true)之前调用过setter,则仅唤醒设备。

I tested this on my Android Pie device.

我在我的 Android Pie 设备上对此进行了测试。

回答by Prativa

getWindow().addFlags(LayoutParams.FLAG_DISMISS_KEYGUARD);

will dismiss the general keyguard and cause the device to unlock.

将关闭通用键盘锁并导致设备解锁。

回答by schar

Settling an alarm programatically will wake up the phone(play a sound) and i guess the turn on display would be an option there.

以编程方式设置闹钟将唤醒手机(播放声音),我想打开显示器将是那里的一个选项。

I donot think there would be an exposed API that will unlock the phone automatically.

我认为不会有公开的 API 可以自动解锁手机。