最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to keep screen OFF when receiving certain key inputs using Java on Android? - Stack Overflow

programmeradmin7浏览0评论

I'm still learning Android/Java development, so I'm still learning the API and what's possible to do.

I'm creating an accessibility service that does things like open apps, turn the light on/off, simulate swipes, etc. based on key sequences received from a wearable device (it's essentially a Bluetooth keyboard). So far, everything is working, except every time it receives key input, it wakes the Android device - which is fine for something like opening an app, since the user will want to see the screen in that case.

The keys that I want to NOT wake the device are the media keys - play/pause, next/previous track, and volume adjustment. The user's Android device will probably be in their pocket the majority of times those keys are used, so waking it is not ideal.

A few attempts:

  • Doing the media control programmatically versus using the actual key-codes. The Bluetooth wearable is currently sending out the actual keycodes for play/pause, skipping tracks, etc. - however, I also tried changing those keys to something else, like F2, and trying to trigger it programmatically within the service. This sometimes has weird effects, though, like the volume keys no longer working if I use something like audioManager.adjustVolume(AudioManager.ADJUST_RAISE, 0); because then the API expects me to handle all volume triggers myself, and play/pause is hard to trigger for multiple different media methods (Spotify vs Youtube, etc.) and I still run into the waking-device issue.

  • Locking the screen again after the key action is completed using

    performGlobalAction(GLOBAL_ACTION_LOCK_SCREEN);
    

    This produces a delay, however, so probably isn't the best option and feels pretty clumsy.

  • Most promising, in my opinion - PowerManager using WakeLock, but this is usually for keeping the screen on, I think. I've managed to get the screen to turn off if the proximity sensor is activated when a media key is pressed using the following:

    powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
    PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK, "myApp:WakeLock");
    wakeLock.acquire(2*1000L /*2 seconds*/);
    

    However, I'm still trying to figure out how this works. The screen usually wakes back up after a media-key-press once the WakeLock expires after the 2 seconds, which makes sense, but I'm not sure how to properly keep it off during/after key-press - I'm unsure of just putting the WakeLock timer at a higher value, especially if it'll run down battery if the WakeLock is technically active for longer periods. I've also tested the flag PowerManager.PARTIAL_WAKE_LOCK but that just wakes the screen again.
    The idea is if one of the media keys is pressed while the proximity sensor is activated, then the screen should stay off - which is not a perfect solution, in my opinion, compared to simply keeping the screen off regardless, but it seemed the more promising option compared to the other attempts.

Any input would be appreciated, especially if I could somehow get the proximity sensor option to work - that way, the screen would hypothetically stay on if the user is actively using the device screen, but automatically stay off if it's in their pocket.

发布评论

评论列表(0)

  1. 暂无评论