Search code examples
androidandroid-sensorslayout-inflater

Android: onshake with password screen


I am implementing a sensoreventlistener.

Whenever the device is shaked I want to provide a password screen.

I need some suggestion on how to implement it. What I am doing is I am inflateing password layout in onShake method can I do that?

The password layout should look like general passcode lock we used in devices..(giving 4 digits)

Any suggestions will be appreciated.

Below is my ShakeListener Activity

public   class ShakeListenerTestActivity extends Activity 
{
  private ShakeListener mShaker;
  private EditText password;

  @Override
  public void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);              


    mShaker = new ShakeListener(this);
    mShaker.setOnShakeListener(new ShakeListener.OnShakeListener () {
      public void onShake()
      {


   Toast.makeText(ShakeListenerTestActivity.this, password.getText(),
            Toast.LENGTH_SHORT).show();

      }

    });

  }

  public void onResume()
  {
    mShaker.resume();
    super.onResume();
  }

  @Override
  public void onPause()
  {
    mShaker.pause();
    super.onPause();  
  } 
}

Solution

  • Yes logically speaking you should be able to inflate a layout in OnShake method , or you could even start a activity containing the password view. In onShake method just start a new activity which contains the password view and then based on the input from user you can decide what to do next !

    Edit:

    How to detect shake event with android?

    Have a look at that & you will find how to detect a shake so after detecting a shake you just need to start a new Activity :)

    Let me know if you need more help

    Edit 2:

    Some code snippets how i think it will work :

    public class ShakeActivity extends Activity implements SensorListener
    
    sensorMgr = (SensorManager) getSystemService(SENSOR_SERVICE);
    
    sensorMgr.registerListener(this,
    SensorManager.SENSOR_ACCELEROMETER,
    SensorManager.SENSOR_DELAY_UI);
    
    public void onSensorChanged(int sensor, float[] values) {
    if (sensor == SensorManager.SENSOR_ACCELEROMETER) {
    long curTime = System.currentTimeMillis();
    // only allow one update every 100ms.
    if ((curTime - lastUpdate) > 100) {
    long diffTime = (curTime - lastUpdate);
    lastUpdate = curTime;
    
    x = values[SensorManager.DATA_X];
    y = values[SensorManager.DATA_Y];
    z = values[SensorManager.DATA_Z];
    
    float speed = Math.abs(x+y+z – last_x – last_y – last_z) / diffTime * 10000;
    
    if (speed > SHAKE_THRESHOLD) {
    Log.d(”sensor”, “shake detected w/ speed: ” + speed);
    Toast.makeText(this, “shake detected w/ speed: ” + speed, Toast.LENGTH_SHORT).show();
    Intent i = new Intent(this, newActivity.class);
    startActivity(i);
    }
    last_x = x;
    last_y = y;
    last_z = z;
      }
     }
    }
    

    That should solve your problem ! Also register the second activity in the Manifest. Hope this helps!

    Also almost the entire code above was from Here , so thanks to original poster !