如何让 Android WakeLock 工作?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2039555/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:18:37  来源:igfitidea点击:

How to get an Android WakeLock to work?

android

提问by Curyous

My WakeLock isn't keeping my device awake.

我的 WakeLock 没有让我的设备保持清醒。

In OnCreate()I've got:

OnCreate()我有:

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
mWakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ON_AFTER_RELEASE, "My Tag");
mWakeLock.acquire();

then:

然后:

new CountDownTimer(1320000, 200) {

    public void onTick(long millisUntilFinished) {
        // I update a progress bar here.                                         
    }

    public void onFinish() {
        // I finish updating the progress bar.
        mWakeLock.release();
    }
}.start();

The screen turns off before the timer finishes, how can I make the screen stay visible?

屏幕在计时器结束前关闭,如何使屏幕保持可见?

mWakeLockis a field previously declared like so:

mWakeLock是一个先前声明的字段,如下所示:

private PowerManager.WakeLock mWakeLock;

My device uses Android 1.6. I would really appreciate any help to resolve this.

我的设备使用 Android 1.6。我真的很感激任何帮助解决这个问题。

回答by Anoop Chandrika HarisudhanNair

WakeLock doesn't usually cause Reboot problems. There may be some other issues in your coding. WakeLock hogs battery heavily, if not released after usage.

WakeLock 通常不会导致重启问题。您的编码中可能还有其他一些问题。WakeLock 会严重占用电池,如果在使用后不释放。

WakeLock is an Inefficient way of keeping the screen on. Instead use the WindowManager to do the magic. The following one line will suffice the WakeLock. The WakeLock Permission is also needed for this to work.Also this code is more efficient than the wakeLock.

WakeLock 是一种保持屏幕开启的低效方式。而是使用 WindowManager 来做魔术。下面一行就足够了 WakeLock。要使其工作,还需要 WakeLock 权限。此外,这段代码比wakeLock 更有效。

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

You need not relase the WakeLock Manually. This code will allow the Android System to handle the Lock Automatically. When your application is in the Foreground then WakeLock is held and else android System releases the Lock automatically.

您无需手动释放 WakeLock。此代码将允许 Android 系统自动处理锁定。当您的应用程序在前台时,WakeLock 被保持,否则 android 系统会自动释放锁。

Try this and post your comment...

试试这个并发表你的评论......

回答by wf.

Do you have the required permissionset in your Manifest?

您的清单中是否设置了所需的权限

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

回答by Maverick

You just have to write this:

你只需要这样写:

 private PowerManager.WakeLock wl;

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

            PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
            wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "DoNjfdhotDimScreen");
    }//End of onCreate

            @Override
        protected void onPause() {
            super.onPause();
            wl.release();
        }//End of onPause

        @Override
        protected void onResume() {
            super.onResume();
            wl.acquire();
        }//End of onResume

and then add permission in the manifest file

然后在清单文件中添加权限

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

Now your activity will always be awake. You can do other things like w1.release()as per your requirement.

现在您的活动将始终保持清醒。您可以w1.release()根据您的要求做其他事情。

回答by Beyaz

Keep the Screen On

保持屏幕开启

First way:

第一种方式:

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

Second way:

第二种方式:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:keepScreenOn="true">
    ...
</RelativeLayout>

Keep the CPU On:

保持 CPU 开启:

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

and

PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyWakelockTag");
wakeLock.acquire();

To release the wake lock, call wakelock.release(). This releases your claim to the CPU. It's important to release a wake lock as soon as your app is finished using it to avoid draining the battery.

要释放唤醒锁,请调用wakelock.release()。这将释放您对 CPU 的声明。在您的应用程序使用完毕后立即释放唤醒锁以避免耗尽电池电量,这一点很重要。

Docs here.

文档在这里

回答by CharlieNorris

Add permission in AndroidManifest.xml, as given below

在 中添加权限AndroidManifest.xml,如下所示

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

preferably BEFORE your <application>declaration tags but AFTER the <manifest>tags, afterwards, try making your onCreate()method contain only the WakeLock instantiation.

最好在你的<application>声明标签之前但在<manifest>标签之后,之后,尝试让你的onCreate()方法只包含 WakeLock 实例化。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    PowerManager pm = (PowerManager)getSystemService(Context.POWER_SERVICE);
    mWakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ON_AFTER_RELEASE, "My Tag");
}

and then in your onResume()method place

然后在你的onResume()方法位置

@Override
public void onResume() {
   mWakeLock.aquire();
}

and in your onFinish()method place

并在您的onFinish()方法位置

@Override
public void onFinish() {
   mWakeLock.release();
}

回答by Sunil Kumar Sahoo

To achieve the same programmatic you can use following

要实现相同的编程,您可以使用以下

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

or adding following in layout also will perform the above task

或在布局中添加以下内容也将执行上述任务

 android:keepScreenOn="true"

The details you can get from following url http://developer.android.com/training/scheduling/wakelock.html

您可以从以下网址获得详细信息 http://developer.android.com/training/scheduling/wakelock.html

I have used combination of following to wake my screen when keyguard locked and keep my screen on

当键盘锁锁定并保持屏幕开启时,我使用以下组合来唤醒我的屏幕

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

回答by Pclasp

{PowerManager mgr = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
WakeLock wakeLock = mgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,"MyWakeLock"); 
wakeLock.acquire();}

use this code and don't forget to permit wakelock in android manifest

使用此代码,不要忘记在 android manifest 中允许唤醒锁

回答by Steven Bennett

Thank you for this thread. I've been having a hard time implementing a Timer in my code for 5 minutes to run an activity, because my phone I have set to screen off/sleep around 2 minutes. With the above information it appears I have been able to get the work around.

谢谢你的这个话题。我一直很难在我的代码中实现 5 分钟的计时器来运行活动,因为我的手机已设置为屏幕关闭/睡眠大约 2 分钟。有了上述信息,我似乎已经能够解决问题。

    @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    /* Time Lockout after 5 mins */
    getWindow().addFlags(LayoutParams.FLAG_KEEP_SCREEN_ON);

    Timer timer = new Timer();
    timer.schedule(new TimerTask() {

       public void run() {

        Intent i = new Intent(AccountsList.this, AppEntryActivity.class);
        i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(i);
        finish();
        return;

       }

    }, 300000); 
    /* Time Lockout END */
 }

回答by Sree Rama

sample code snippet from android developers site

来自android 开发者站点的示例代码片段

public class MainActivity extends Activity {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
  }

Best Practices for Background Jobs

后台作业的最佳实践

回答by Archit

Try using the ACQUIRE_CAUSES_WAKEUP flag when you create the wake lock. The ON_AFTER_RELEASE flag just resets the activity timer to keep the screen on a bit longer.

创建唤醒锁时尝试使用 ACQUIRE_CAUSES_WAKEUP 标志。ON_AFTER_RELEASE 标志只是重置活动计时器以使屏幕保持更长的时间。

http://developer.android.com/reference/android/os/PowerManager.html#ACQUIRE_CAUSES_WAKEUP

http://developer.android.com/reference/android/os/PowerManager.html#ACQUIRE_CAUSES_WAKEUP