2

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();  
  } 
}
Bruno Bieri
  • 9,724
  • 11
  • 63
  • 92
teekib
  • 2,821
  • 10
  • 39
  • 75

2 Answers2

0

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 !

Community
  • 1
  • 1
Adnan Mulla
  • 2,872
  • 3
  • 25
  • 35
  • @Adnana ...Thank you...any suggestion on how to make the passcode lock layout look – teekib Nov 14 '12 at 07:27
  • tried this ..but intent is no starting public void onShake() { vibe.vibrate(100); Intent myIntent = new Intent(ShakeListenerTestActivity.this, passwordActivity.class); ShakeListenerTestActivity.this.startActivity(myIntent); Toast.makeText(ShakeListenerTestActivity.this, password.getText(), Toast.LENGTH_SHORT).show(); } }); – teekib Nov 14 '12 at 07:33
  • How are you detecting the movement? You need to use accelerometer to detect the movement of the phone . Have a look at the link in my answer. I edited it now. – Adnan Mulla Nov 14 '12 at 09:22
  • i am using accelerometer ..here s my code http://pastebin.com/S3BJXsam and trying to start this activity http://pastebin.com/f1tWULgi – teekib Nov 14 '12 at 09:27
  • So whats the problem ? Also i dont see you registering the Listener is on Create of SensorTestActivity , you have just registered the listener in onResume ! – Adnan Mulla Nov 14 '12 at 09:45
  • i registered the listener , al;so registered the second activity...but still getting force close.. – teekib Nov 14 '12 at 10:28
  • If your problem is not yet solved and if your still getting FC then post the Logcat here ! – Adnan Mulla Nov 14 '12 at 11:50
  • yeahh..ill try and if unsuccessful ill post the log..Thank you – teekib Nov 14 '12 at 12:15
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/19533/discussion-between-adnan-mulla-and-teekib) – Adnan Mulla Nov 14 '12 at 13:33
0

Here is complete code of shake password view on wrong entry in android like in iOS

Animation shake = AnimationUtils.loadAnimation(this, R.anim.shake);
 findViewById(R.id.pwd).startAnimation(shake);

and here is its shake.xml from anim folder

<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="500"
android:fromXDelta="0"
android:interpolator="@anim/cycle_7"
android:toXDelta="10" />

for more info see the link below https://github.com/MShoaibAkram/AnimateViewOnWrongPassword