GIgio2k
GIgio2k

Reputation: 36

unlock android not work with FLAG_DISMISS_KEYGUARD

this is my code, copy and pasted from other topics about unlock an android.. i don't know why this not work ! :(

The goal is : when the proximity sensor is HI , the device unlock itself.

(more info: Using proximity sensor lock and unlock a home screen)

Sensor work fine, but the phone still locked :(

I don't have pin lock or other , i deploy on a nexus 5.

private SensorManager mSensorManager;       
private Sensor ProxymitySensor ;
private ToneGenerator tg = new ToneGenerator(AudioManager.STREAM_NOTIFICATION, 100);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_unlock);

    mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);  
    ProxymitySensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
    mSensorManager.registerListener(this, ProxymitySensor,SensorManager.SENSOR_DELAY_NORMAL);


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.unlock, menu);
    return true;
}


  @Override
  public final void onSensorChanged(SensorEvent event) {
    Object sensordata = event.values[0];
    // Do something with this sensor data.
    if (event.values[0] == event.sensor.getMaximumRange()){     
                    PlaySound();
                    UnlockMe();
    }
  }

  private void PlaySound() {           
         tg.startTone(ToneGenerator.TONE_PROP_BEEP);
  }

  private void UnlockMe(){
      // try to unlock the phone 
       getWindow().addFlags(LayoutParams.FLAG_DISMISS_KEYGUARD);
       getWindow().addFlags(LayoutParams.FLAG_SHOW_WHEN_LOCKED);
       getWindow().addFlags(LayoutParams.FLAG_TURN_SCREEN_ON);         
       // nothing happen :( 
  }



  @Override
  public void onAccuracyChanged(Sensor arg0, int arg1) {
    // TODO Auto-generated method stub          
  }

here the complete file : http://pastebin.com/CCv2W70Y

my fone is locked , if i put my hand on it i listen the beep and nothing more :( LocgCat don't say anityng...

any idea ?

Upvotes: 1

Views: 2902

Answers (1)

ignoramous
ignoramous

Reputation: 89

You're missing the full screen flag.

From Google's documentation:

public static final int FLAG_SHOW_WHEN_LOCKED

Window flag: special flag to let windows be shown when the screen is locked. This will let application windows take precedence over key guard or any other lock screens. Can be used with FLAG_KEEP_SCREEN_ON to turn screen on and display windows directly before showing the key guard window. Can be used with FLAG_DISMISS_KEYGUARD to automatically fully dismisss non-secure keyguards. This flag only applies to the top-most full-screen window.

Emphasis mine.

Upvotes: 4

Related Questions