如何在我的应用程序中保持屏幕打开?

对于我的Android应用程序,我永远不想手机锁定或关闭背光



紫衣仙女
浏览 509回答 3
3回答

UYOU

使用PowerManager.WakeLock类可以执行此操作。请参见以下代码:import android.os.PowerManager;public class MyActivity extends Activity {&nbsp; &nbsp; protected PowerManager.WakeLock mWakeLock;&nbsp; &nbsp; /** Called when the activity is first created. */&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onCreate(final Bundle icicle) {&nbsp; &nbsp; &nbsp; &nbsp; setContentView(R.layout.main);&nbsp; &nbsp; &nbsp; &nbsp; /* This code together with the one in onDestroy()&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* will make the screen be always on until this Activity gets destroyed. */&nbsp; &nbsp; &nbsp; &nbsp; final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);&nbsp; &nbsp; &nbsp; &nbsp; this.mWakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag");&nbsp; &nbsp; &nbsp; &nbsp; this.mWakeLock.acquire();&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onDestroy() {&nbsp; &nbsp; &nbsp; &nbsp; this.mWakeLock.release();&nbsp; &nbsp; &nbsp; &nbsp; super.onDestroy();&nbsp; &nbsp; }}在清单文件中使用以下权限:<uses-permission android:name="android.permission.WAKE_LOCK" />希望这能解决您的问题... :)

小唯快跑啊

在onCreate()中的setContentView()之后添加一行代码public void onCreate(Bundle savedInstanceState) {&nbsp; &nbsp; &nbsp; &nbsp; super.onCreate(savedInstanceState);&nbsp; &nbsp; &nbsp; &nbsp; setContentView(R.layout.activity_flag);&nbsp; &nbsp; &nbsp; &nbsp; getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Android