简体   繁体   English

如何使用PowerManager和PARTIAL_WAKE_LOCK

[英]How to use the PowerManager and PARTIAL_WAKE_LOCK

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,我的问题是,如果我使用此命令正确,我的结构是> myActivity.class,ReceiverBoot.class和ServiceBoot.class,我在myActivity.class上使用POwerManager类,如下所示:

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

on onCreateMethod after of this one put 放在onCreateMethod之后

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

on Manifest XML code I have 在清单XML代码上

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

and on layout XML code have 并在布局上的XML代码具有

android:keepScreenOn="true"

but after to 10seg the creen es OFF always but the app is running, jut with wifi. 但是直到10seg crees总是关闭,但应用程序正在运行时,才可以使用wifi进行投影。

the app work very fine with wifi conn, but when change to 3G conn, the app is gone, I use fine this command?? 该应用程序可以很好地与wifi conn一起工作,但是当更改为3G conn时,该应用程序不见了,我使用了此命令。 the problem is the kind of conn to Internet??? 问题是连接到Internet的那种方式??? thanks a lot! 非常感谢!

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?). (或者如果屏幕问题是您要尝试解决的数据问题?)。

For the screen 对于屏幕

You are not using the right lock to keep the screen on. 您没有使用向右锁来保持屏幕打开。 PARTIAL_WAKE_LOCK only requests that you can use the processor. PARTIAL_WAKE_LOCK仅请求您可以使用处理器。 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. 要使屏幕保持在您的应用程序上,请根据您的需要使用SCREEN_DIM_WAKE_LOCKSCREEN_BRIGHT_WAKE_LOCKFULL_WAKE_LOCK This lock should be held for as long as you need the lock. 该锁应在您需要的时间内一直保持。 Currently you are releasing it in onCreate(). 当前,您正在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). 请记住,即使释放了锁定,用户仍按下电源按钮(PARTIAL除外)。

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: 您可以通过将其添加到onCreate重写中来实现:

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

This is the same effect as using android:keepScreenOn="true" , which you already seem to be doing. 这与使用android:keepScreenOn="true"效果相同,您似乎已经在这样做了。 I can't however tell why that isn't working from your snippets. 但是,从您的片段中我无法分辨出为什么这不起作用。 Make sure you are inflating the right layout. 确保您要充气正确的布局。

For your data 为您的数据

The device will likely be switching off 3G data when the screen is not active (and no lock is present). 当屏幕不活动(并且不存在锁定)时,设备可能会关闭3G数据。 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). 同样,不要在需要时释放锁(尽管也不要永远保持它的锁,这只会消耗手机电池的电量)。

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

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