2017-06-25 3 views

答えて

0

android.permission.WAKE_LOCK権限を使用できます。

まず、この権限をマニフェストに追加します。

あなたの活動に
<uses-permission android:name="android.permission.WAKE_LOCK" /> 

import android.os.PowerManager; 

public class MyActivity extends Activity { 

    protected PowerManager.WakeLock mWakeLock; 

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

それとも、setContentView活動する前にこのコードを追加することができます。このように:

@Override 
public void onCreate(final Bundle icicle) { 
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 
    setContentView(R.layout.main); 
} 
関連する問題