When the screen is turned on, I want to check is power button activated it, if yes, it will auto dismiss the keyguard and run the toast.
When the screen is turned off, the keyguard will be re-enabled. (the code works till here)
However, when the screen is OFF, and i pressed "Volume up" button, the screen turns ON. (but it goes into the loop where it detects "power" button is pressed).
The case should be it shall not activate "Dismiss keyguard" when other buttons(except "power" button) are pressed.
any help is much appreciated to solve this bug :)
another question - how do I use FLAG_DISMISS_KEYGUARD in Service?
public class myService extends Service{
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
super.onCreate();
}
@Override
public void onStart(Intent intent, int startId) {
boolean screenOn = intent.getBooleanExtra("screen_state", false);
boolean pressed = false;
Vibrator myVib;
//screen is turned on
if (!screenOn)
{
pressed = onKeyDown(26, null);
//if it turned on by power button. bug = always go into this loop
if(pressed)
{
Context context = getApplicationContext();
CharSequence text = "Service started. power button pressed";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
//dimiss keyguard
KeyguardManager keyguardManager = (KeyguardManager)getSystemService(Activity.KEYGUARD_SERVICE);
KeyguardLock lock = keyguardManager.newKeyguardLock(KEYGUARD_SERVICE);
lock.disableKeyguard();
Context context2 = getApplicationContext();
CharSequence text2 = "SCREEN ON";
int duration2 = Toast.LENGTH_SHORT;
Toast toast2 = Toast.makeText(context2, text2, duration2);
toast2.show();
}
else
{
Context context = getApplicationContext();
CharSequence text = "Service started. NOT power button pressed";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
}
//screen is turned off
else {
myVib = (Vibrator) this.getSystemService(VIBRATOR_SERVICE);
myVib.vibrate(500);
KeyguardManager keyguardManager = (KeyguardManager)getSystemService(Activity.KEYGUARD_SERVICE);
KeyguardLock lock = keyguardManager.newKeyguardLock(KEYGUARD_SERVICE);
lock.reenableKeyguard();
}
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_POWER)
return true;
else
return false;
}
}//end of myService class