2

I want to turn the android screen on button click,Now i had written a program for this in is not showing any errors also its not working..

The code for this is..

public class MainActivity extends Activity {

Button powerOff;
int amountOfTime =20*1000;
Context context = this;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);




    powerOff = (Button)findViewById(R.id.button1);
    powerOff.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {


            // TODO Auto-generated method stub
            PowerManager.WakeLock mWLock;
             try {
                  System.out.println("Enter try Block");

                  PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
                  mWLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK |
                          PowerManager.ACQUIRE_CAUSES_WAKEUP |
                          PowerManager.ON_AFTER_RELEASE, "WakeLock");
                  mWLock.acquire();
             } catch(Exception e) {
                    Log.e("ScreenLock", "onStart()::acquire() failed " + e.toString());
             }


        }
    });


}

I want to lock the screen and how can i do it???

Geethu
  • 1,586
  • 6
  • 21
  • 34
  • check this out: http://stackoverflow.com/questions/3594532/how-to-programmaticaly-lock-screen-in-android – jelies Jan 15 '13 at 07:34

2 Answers2

2

Use the following code

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "My Tag"); wl.acquire();

in activity you have to release wake lock

wl.release();

use the following permission in manifest

uses-permission android:name="android.permission.WAKE_LOCK"

Yogesh Tatwal
  • 2,722
  • 20
  • 43
0

I guess you have added permission uses-permission android:name="android.permission.WAKE_LOCK"

android2013
  • 415
  • 2
  • 5
  • Maybe your combinations are not right , try using for test if (wakeLock == null) { PowerManager oPowerMngr = (PowerManager)getSystemService(Context.POWER_SERVICE); wakeLock = oPowerMngr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "My Tag"); wakeLock.acquire(); } – android2013 Jan 15 '13 at 08:49