唤醒 Android 设备

发布于 2024-09-17 12:29:04 字数 73 浏览 5 评论 0原文

嘿,我需要在某个时间唤醒我正在睡觉的 Android 设备。 有什么建议吗?

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

许仙没带伞 2024-09-24 12:29:05

唤醒屏幕:

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

释放屏幕锁定:

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

并且清单文件需要包含:

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

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

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

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

EDIT: this answer is reported as deprecated.

追风人 2024-09-24 12:29:05

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

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

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

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

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

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 screen in 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.

差↓一点笑了 2024-09-24 12:29:05

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

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

在您的活动 OnCreate 中输入类似以下内容:

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

它将在 Screen 2 秒后启动处理程序关闭事件。

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

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

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

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

在清单文件中请求权限:

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

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

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

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

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

In your actiivity OnCreate put something like:

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.

Register receiver in your onResume() method:

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)

unregisterReceiver(mReceiver);
Log.i(TAG, "broadcast UNregistred!");
网名女生简单气质 2024-09-24 12:29:05

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

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")
                }
            }
        )
    }
}

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

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

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.requestDismissKeyguard only wakes up the device, if the setter setTurnScreenOn(true) was called before.

I tested this on my Android Pie device.

焚却相思 2024-09-24 12:29:05

在活动 onCreate() 方法中的 setContentView(R.layout.YOUR_LAYOUT); 之后尝试使用以下代码

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
        Log.d(TAG, "onCreate: set window flags for API level > 27");
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN
                        | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        KeyguardManager keyguardManager = (KeyguardManager) getApplicationContext().getSystemService(Context.KEYGUARD_SERVICE);
        keyguardManager.requestDismissKeyguard(this, null);
        setShowWhenLocked(true);
        setTurnScreenOn(true);
    } else {
        Log.d(TAG, "onCreate: onCreate:set window flags for API level < 27");
        getWindow().addFlags(
                WindowManager.LayoutParams.FLAG_FULLSCREEN
                        | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
                        | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
                        | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                        | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
    }

Try with the below code after setContentView(R.layout.YOUR_LAYOUT); in activity onCreate() method

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
        Log.d(TAG, "onCreate: set window flags for API level > 27");
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN
                        | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        KeyguardManager keyguardManager = (KeyguardManager) getApplicationContext().getSystemService(Context.KEYGUARD_SERVICE);
        keyguardManager.requestDismissKeyguard(this, null);
        setShowWhenLocked(true);
        setTurnScreenOn(true);
    } else {
        Log.d(TAG, "onCreate: onCreate:set window flags for API level < 27");
        getWindow().addFlags(
                WindowManager.LayoutParams.FLAG_FULLSCREEN
                        | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
                        | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
                        | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                        | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
    }
阿楠 2024-09-24 12:29:05

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

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

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);
}
So要识趣 2024-09-24 12:29:05

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

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

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.

稚然 2024-09-24 12:29:05
getWindow().addFlags(LayoutParams.FLAG_DISMISS_KEYGUARD);

将解除通用键盘保护并导致设备解锁。

getWindow().addFlags(LayoutParams.FLAG_DISMISS_KEYGUARD);

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文