如何使用电源管理和PARTIAL_WAKE_LOCK [英] How to use the PowerManager and PARTIAL_WAKE_LOCK

查看:217
本文介绍了如何使用电源管理和PARTIAL_WAKE_LOCK的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发一个小的应用程序,这其中有当设备处于睡眠或深度睡眠(preSS右边的按钮关闭屏幕)我读了很多关于它的帖子运行总是在一切,都告诉我,该方法是利用电源管理,我的问题是,如果我用细此命令,我的结构> myActivity.class,ReceiverBoot.class和ServiceBoot.class,我用的电源管理类上myActivity.class是这样的:

I develop a small app, this one have to run always over all when the device is sleep or deep sleep (press right button to turn off the screen) I read many posts about it, and all tell me that the way is use PowerManager, and my question is if I use fine this command, my structure is> myActivity.class, ReceiverBoot.class and ServiceBoot.class, I use the POwerManager class on myActivity.class like this:

PowerManager mgr = (PowerManager)this.getSystemService(Context.POWER_SERVICE);
PowerManager wakeLock = mgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP , "MyWakeLock");

在此onCreateMethod看跌后,

on onCreateMethod after of this one put

wakeLock.acquire();

而这一次后,我有一个

and after of this one I have a

super.onCreate(savedInstanceState);
this.mTimer.scheduleAtFixedRate(
                new TimerTask(){
                    @Override
                    public void run(){doTask();}
                } , 0, 1000);

wakeLock.release();

在清单XML code我有

on Manifest XML code I have

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

和布局上的XML code有

and on layout XML code have

android:keepScreenOn="true"

但后10seg的creen ES OFF始终,但应用程序正在运行,带wifi突出部分。

but after to 10seg the creen es OFF always but the app is running, jut with wifi.

应用程序工作很细带wifi康涅狄格州,但是当切换到3G康恩,应用程序走了,我用细这个命令?问题是那种conn将互联网的?非常感谢!

the app work very fine with wifi conn, but when change to 3G conn, the app is gone, I use fine this command?? the problem is the kind of conn to Internet??? thanks a lot!

推荐答案

我不是100%清楚你的问题。无论是其数据的问题,或屏幕的问题。 (或者,如果屏幕问题是你在做什么,试图解决数据问题?)。

I'm not 100% clear on your issue. Whether its the data issue, or the screen issue. (Or if the screen issue is what you are doing to try and fix the data issue?).

对于屏幕

您没有使用正确的锁,以保持在屏幕上。 PARTIAL_WAKE_LOCK 只可以使用处理器的请求。为了使屏幕上的应用程序中使用 SCREEN_DIM_WAKE_LOCK SCREEN_BRIGHT_WAKE_LOCK之一 FULL_WAKE_LOCK 取决于你想要什么。此锁应当保持,只要你需要的锁。您正在释放它的onCreate()。请记住,如果用户$ P $虽然psses电源按钮,你的锁被释放(偏作为例外)。

You are not using the right lock to keep the screen on. PARTIAL_WAKE_LOCK only requests that you can use the processor. To keep the screen on your app use one of SCREEN_DIM_WAKE_LOCK, SCREEN_BRIGHT_WAKE_LOCK or FULL_WAKE_LOCK depending on what you want. This lock should be held for as long as you need the lock. Currently you are releasing it in onCreate(). Keep in mind that if the user presses the power button though that your lock is released (with PARTIAL being the exception to this).

如果你的目的只是为了保持屏幕时的看法是活跃在那么最好不要在所有使用该锁。唤醒锁需要额外的许可。您可以通过添加以下内容到的onCreate 重写做到这一点:

If your intent is just to keep the screen on when a view is active then it's better not to use the lock at all. The wake lock needs an extra permission. You can do it by adding this to your onCreate override:

getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

这是由于使用的android一样的效果:keepScreenOn =真正的,你似乎已经在做。我不能告诉但为什么不从你的片段工作。请确保您膨胀正确的布局。

This is the same effect as using android:keepScreenOn="true", which you already seem to be doing. I can't however tell why that isn't working from your snippets. Make sure you are inflating the right layout.

为您的数据

该设备可能会被关掉3G数据时,屏幕不处于活动状态(无锁present)。再次,不释放你的锁,如果你需要它(虽然不把它永远不是,这只是要吸取手机电池)。

The device will likely be switching off 3G data when the screen is not active (and no lock is present). Again, don't release your lock if you need it (Though don't keep it forever either, that's just going to suck up phone battery).

这篇关于如何使用电源管理和PARTIAL_WAKE_LOCK的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆