简体   繁体   English

如何在Kivy应用程序(Android)中获取PARTIAL_WAKE_LOCK?

[英]How to acquire PARTIAL_WAKE_LOCK in a Kivy app (Android)?

I'm trying to acquire a PARTIAL_WAKE_LOCK for my kivy app. 我试图获取PARTIAL_WAKE_LOCK我kivy应用程序。 Basically it is a timer app, the timer should continue to run in the background when the screen has turned off. 基本上它是一个计时器应用程序,当屏幕关闭时,计时器应继续在后台运行。 Everything is working fine, except for the wake lock. 一切都工作正常,除了唤醒锁。 This is how i would implement it in python: 这是我将如何在python中实现它:

from jnius import autoclass

PythonActivity = autoclass('org.renpy.android.PythonActivity')
activity = PythonActivity.mActivity

Context = autoclass('android.content.Context')
PowerManager = autoclass('android.os.PowerManager')

pm = activity.getSystemService(Context.POWER_SERVICE)
wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, 'TAG')

wl.acquire()

The app runs fine, but when the screen turns off it crashes. 该应用程序运行正常,但当屏幕关闭它崩溃。 Applying the 应用

def on_pause(self):
    return True
def on_resume(self):
    pass

method doesn't help me, because it stops the timer at the point where the screen turns off. 方法对我没用,因为它会在屏幕关闭的位置停止计时器。

In my buildozer.spec file i have: 在我的buildozer.spec文件中,我有:

android.permissions = WAKE_LOCK

When i set: 当我设置:

android.wakelock = True

i only get the screen_bright_wakelock , but i need the partial_wakelock . 我只得到screen_bright_wakelock ,但我需要partial_wakelock

I guess your misunderstood the WAKE_LOCK here. 我想你在这里误解了WAKE_LOCK By default, if you don't touch the application, the system will detect the phone as Idle, and lock it. 默认情况下,如果您未触摸应用程序,系统会将手机检测为空闲,然后将其锁定。

If the user close the screen, switch to another app, etc, WAKE_LOCK will not do anything. 如果用户关闭屏幕,切换到另一个应用程序等, WAKE_LOCK将不会执行任何操作。 Because at that point, your application must be in background, and in a "sleep" mode. 因为此时,您的应用程序必须处于后台,并处于“睡眠”模式。

What you need is a service. 你需要的是一项服务。 A Service can continue running when the application is in background, screen locked or not. 当应用程序处于后台,屏幕锁定或未锁定时,服务可以继续运行。 And you need communication link between your app and the service (internal OSC can be ok, feel free to search examples on the internet). 并且您需要在您的应用和服务之间建立通信链接(内部OSC可以正常,随时可以在互联网上搜索示例)。

Or, try to see if AlarmManager / Alarm from the Android API can be ok with you (never tested). 或者,尝试查看Android API中的AlarmManager / Alarm是否适合您(从未测试过)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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