如何在Android上始终保持屏幕

时间:2014-04-29 06:59:52

标签: android

我正在构建一个Android应用程序,其中我希望屏幕(活动)在一段时间之后没有锁定意味着应用程序屏幕始终打开。如何在我的应用程序中执行此操作以保持所有屏幕始终打开。意思是没有屏幕没有锁定屏幕在我的申请中克服。

4 个答案:

答案 0 :(得分:1)

只需在onCreate方法

中添加此行
  

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

它会使屏幕处于活动状态。

答案 1 :(得分:0)

执行以下操作,

protected PowerManager.WakeLock wakelock;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(final Bundle savedInstances) {
        setContentView(R.layout.main);

        final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
        this.wakelock= pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag");
        this.wakelock.acquire();
    }

    @Override
    public void onDestroy() {
        this.wakelock.release();
        super.onDestroy();
    }

不要忘记在清单文件中添加以下权限:

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

答案 2 :(得分:0)

尝试以下代码: -

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

import android.os.PowerManager;

public class MyActivity extends Activity {

protected PowerManager.WakeLock mWakeLock;

/** Called when the activity is first created. */
@Override
public void onCreate(final Bundle icicle) {
    setContentView(R.layout.main);

    /* This code together with the one in onDestroy() 
     * will make the screen be always on until this Activity gets destroyed. */
    final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    this.mWakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag");
    this.mWakeLock.acquire();
}

@Override
public void onDestroy() {
    this.mWakeLock.release();
    super.onDestroy();
}
}

了解更多信息,请参阅以下链接: -

How do I keep the screen on in my App?

Android disable screen timeout while app is running

答案 3 :(得分:0)

以下是保持屏幕开启的方法: http://developer.android.com/training/scheduling/wakelock.html