-2

I am Building a android application in which i want screen(activity) not locked after some time means application screen always on.How can i do this in my application for all screen keep always on.means no screen ssaver no lock screen overcome in my application.

user3541223
  • 23
  • 1
  • 3
  • 1
    You can refer to this post, next time search. [Solution][1] [1]: http://stackoverflow.com/questions/5712849/how-do-i-keep-the-screen-on-in-my-app – noidraug Apr 29 '14 at 07:03

4 Answers4

6

try below code:-

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

or

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

for more info see below links:-

How do I keep the screen on in my App?

Android disable screen timeout while app is running

Community
  • 1
  • 1
duggu
  • 37,851
  • 12
  • 116
  • 113
  • thankyou sir one more question in i want when phone start my application run with phone boot.doese not exit from my application while phone switch off – user3541223 Apr 29 '14 at 07:33
  • @user3541223 not understand your requirement. – duggu Apr 29 '14 at 07:39
  • My requirement is when android phone is turned on run my application only on phone startup.and user never exit from my application until phone is turned off – user3541223 Apr 29 '14 at 07:44
2

simply add this line in onCreate method

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

It will keeps screen in active state.

Shailendra Madda
  • 20,649
  • 15
  • 100
  • 138
0

Do something like below,

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

don't forget to add the following permission in manifest file :

<uses-permission android:name="android.permission.WAKE_LOCK" />
Spring Breaker
  • 8,233
  • 3
  • 36
  • 60
0

These are the ways to keep the screen on: http://developer.android.com/training/scheduling/wakelock.html

Karakuri
  • 38,365
  • 12
  • 84
  • 104