Android - 唤醒锁无法正确获取,应用程序需要在待机状态下保持运行

发布于 2024-10-17 23:27:29 字数 1579 浏览 3 评论 0原文

在我的应用程序中,在主活动的 onCreate() 方法中,我创建了一个唤醒锁,以便在手机处于待机状态/屏幕关闭时 CPU 将继续运行。

另外,在 onCreate 方法中,我打算创建一个使用加速度计的服务。该服务需要在应用程序打开时持续运行并监视加速计值(我知道这对电池不利,但我需要它来做到这一点)。这是我目前的代码,服务启动正常。

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);  

        PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
        PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Howaya");
        wl.acquire();

        if (appStart == true)  
        { 
            Intent AccelService = new Intent(this, Accelerometer.class);
            AccelService.putExtra("unreg", false);
            startService(AccelService);
        }
        appStart = false;
  }

我在清单中设置了以下权限 -

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

我已经尝试使用不同的锁 - 暗淡的屏幕和全亮度也无济于事。我在 logcat 上的输出在这里 -

F/PowerManager(15628): android.util.Log$TerribleFailure: WakeLock finalized while still held: Howaya
F/PowerManager(15628):  at android.util.Log.wtf(Log.java:260)
F/PowerManager(15628):  at android.util.Log.wtf(Log.java:239)
F/PowerManager(15628):  at android.os.PowerManager$WakeLock.finalize(PowerManager.java:329)
F/PowerManager(15628):  at dalvik.system.NativeStart.run(Native Method)

我看到有人说部分唤醒锁不能像他们应该做的那样工作,例如此链接 Google 待机错误页面 但这是去年发布并关闭的,所以我不知道情况是否如此,有人可以在这里提供帮助吗?关于最后一点,我也有一个 HTC Desire,谢谢。

In my app, in the onCreate() method of the main activity I am creating a wake lock so that the CPU will keep running if the phone goes on standb/screen turns off.

Also in the onCreate method I have an intent to create a service that uses the accelerometer. This service needs to be continuously running while the app is open and monitoring accelerometer values (I know this isn't good for battery but I need it to do that). Here is my code at the moment and the service starts fine.

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);  

        PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
        PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Howaya");
        wl.acquire();

        if (appStart == true)  
        { 
            Intent AccelService = new Intent(this, Accelerometer.class);
            AccelService.putExtra("unreg", false);
            startService(AccelService);
        }
        appStart = false;
  }

I have the following permission set in my manifest -

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

I have tried this with different locks - dim screen and full brightness to no avail too. My output on logcat is here -

F/PowerManager(15628): android.util.Log$TerribleFailure: WakeLock finalized while still held: Howaya
F/PowerManager(15628):  at android.util.Log.wtf(Log.java:260)
F/PowerManager(15628):  at android.util.Log.wtf(Log.java:239)
F/PowerManager(15628):  at android.os.PowerManager$WakeLock.finalize(PowerManager.java:329)
F/PowerManager(15628):  at dalvik.system.NativeStart.run(Native Method)

I have seen people saying that partial wakelocks don't work like they should do, such as this link Google standby error page but this was released and closed last year so I don't know is that the case, can anyone help here please? I have a HTC Desire as regards that last point too, Thanks.

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

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

发布评论

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

评论(3

戏舞 2024-10-24 23:27:29

出现问题是因为您的 WakeLock 对象是 OnCreate 方法内的局部范围变量。执行方法后 - WakeLock 对象不再被引用 - 因此符合垃圾回收的条件。如果发生 Dalvik GC - 对象已准备好进行终结 - 并且终结器内部代码警告您,WakeLock 仍处于保留状态 - 并且处于活动状态 - 但将被 GC 处理。您必须获取新的 WakeLock 对象并将其分配给您的活动派生类中的类类型 WakeLock 的字段。阅读有关面向对象编程和垃圾收集器的内容 - 您将了解这个问题。

Problem occurs because your WakeLock object is a local scoped variable inside OnCreate method. After method being executed - WakeLock object is no more referenced - thus eligible for garbage collection. If Dalvik GC occurs - object is ready for finalize - and finalizer internal code warns You, that WakeLock is still held - and active - but will be GC'ed. You have to obtain new WakeLock object and assign it to a field of class type WakeLock in Your activity derived class. Read about Object Oriented programming and Garbage Collector - You will understand the issue.

开始看清了 2024-10-24 23:27:29

在我的应用程序中,在主 Activity 的 onCreate() 方法中,我创建了一个唤醒锁,以便在手机处于待机状态/屏幕关闭时 CPU 将继续运行。

请在布局中的某个小部件上使用 android:keepScreenOn。对于活动来说,这比手动处理 WakeLock 安全得多。另外,IIRC,您不需要 WAKE_LOCK 权限。

我在 logcat 上的输出在这里

该错误是因为您从未释放 WakeLock。请不要泄露WakeLocks

现在,您成功地写出了所有这些散文并包含了所有这些列表,但您忘记了一件小事:实际上告诉我们您的问题是什么。对于像 StackOverflow 这样的问答网站来说,这是一个相当重要的项目。而且,不,“有人可以帮忙吗?”如果您从未定义您希望获得什么“帮助”,则不算是一个问题。

In my app, in the onCreate() method of the main activity I am creating a wake lock so that the CPU will keep running if the phone goes on standb/screen turns off.

Please use android:keepScreenOn on one of the widgets in your layout instead. That is far, far safer for activities than manually dealing with a WakeLock. Plus, you don't need the WAKE_LOCK permission then, IIRC.

My output on logcat is here

That error is because you never release the WakeLock. Please do not leak WakeLocks.

Now, you managed to write all of this prose and include all of these listings, and you forgot one little thing: actually telling us what your question is. This is a rather important item for a question-and-answer site like StackOverflow. And, no, "can anyone help here please?" does not count as a question when you never define what "help" it is you are looking to receive.

淡紫姑娘! 2024-10-24 23:27:29

正如 @Michal P 所说,您在 onCreate 方法中获取了唤醒锁,但从未释放它。

当GC扫描这个零引用对象时,调用默认的finalize方法,但实际上它是活动的。

  @Override
    protected void finalize() throws Throwable {
        synchronized (mToken) {
            if (mHeld) {
                Log.wtf(TAG, "WakeLock finalized while still held: " + mTag);
                Trace.asyncTraceEnd(Trace.TRACE_TAG_POWER, mTraceName, 0);
                try {
                    mService.releaseWakeLock(mToken, 0);
                } catch (RemoteException e) {
                }
            }
        }
    }

As @Michal P said, you acquire a wakelock in onCreate method, but you never release it.

When GC scan this zero referenced object, call default finalize method, but actually it is active.

  @Override
    protected void finalize() throws Throwable {
        synchronized (mToken) {
            if (mHeld) {
                Log.wtf(TAG, "WakeLock finalized while still held: " + mTag);
                Trace.asyncTraceEnd(Trace.TRACE_TAG_POWER, mTraceName, 0);
                try {
                    mService.releaseWakeLock(mToken, 0);
                } catch (RemoteException e) {
                }
            }
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文