Open top menu








Example:-Prevent the android device Screen going to sleep (foreground) ?

First you can given Manifest Permission

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


DeviceStayWake .java


import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.PowerManager;
import android.support.annotation.Nullable;

public class DeviceStayWake extends Activity {

    protected PowerManager.WakeLock mWakeLock;

    @Override    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
        this.mWakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "Tag");
        this.mWakeLock.acquire();
    }

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






Tagged

0 comments