I've written an android program involving a counter that is increased by one when the volume button is pressed. It's also supposed to turn the screen off when this button is pressed and wait 5 seconds of inactivity before turning it back on. But nothing is happening besides the counter increasing when I run the application, i.e. the screen doesn't turn off.
Here is my java class:
public class MainActivity extends Activity {
private TextView counterTextView;
int count;
PowerManager.WakeLock wakeLock;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
counterTextView = (TextView) findViewById(R.id.counterTextView);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_VOLUME_UP && event.getRepeatCount() == 0) {
count++;
counterTextView.setText(" " + count + " ");
PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
"MyWakelockTag");
wakeLock.acquire();
new Handler().postDelayed(screenOnRunnable(), 5000);
return true;
}
return super.onKeyDown(keyCode, event);
}
private Runnable screenOnRunnable() {
return new Runnable() {
@Override
public void run() {
wakeLock.release();
}
};
}
}
I also added the WAKE_LOCK permission in the manifest:
<uses-permission android:name="android.permission.WAKE_LOCK" />
I have no idea what's wrong... any help would be greatly appreciated.