Search code examples
androidsensorsshake

Wake up the screen if device detects the shake event


I'm registering the sensor after the screen sleeps.I have to wake up the screen if device detects the shake event. If i connect the device with the system, device wakes up correctly. if i disconnect the device, moto g device, chroma tablet doesn't wake up. But samsung device wakes up.

So that I have tested by displaying the logcat value in textview. I found the following problems: Sometimes after screen off, i didn't receive the broadcast message. Sometimes i receive the message, but maccel values didn't increase after shaking the device also.

I'm using the following code:

For initialising sensor manager:

manager=(SensorManager) getSystemService(Context.SENSOR_SERVICE);
sensor=manager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

Registering Broadcast receiver:

registerReceiver(screenoffreceiver, new IntentFilter(Intent.ACTION_SCREEN_OFF));

Broadcast receiver :

private BroadcastReceiver screenoffreceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub
            Bundle extras = intent.getExtras();
            if (Intent.ACTION_SCREEN_OFF.equalsIgnoreCase(intent.getAction())) {
                //screen has been switched off!
                manager.registerListener(Testing.this, manager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);

            }
        }
    };

For Detecting shake event:

public void onSensorChanged(SensorEvent event) {
          float x = event.values[0];
          float y = event.values[1];
          float z = event.values[2];
          mAccelLast = mAccelCurrent;
          mAccelCurrent = (float) Math.sqrt((double) (x*x + y*y + z*z));
          float delta = mAccelCurrent - mAccelLast;
          mAccel = mAccel * 0.9f + delta; // perform low-cut filter
          Log.i("mAccel", "mAccel "+mAccel);

          mytext.setText(mytext.getText().toString()+" "+mAccel);

            if(mAccel>=1) {
                unlock_screen();
            }

Please help me to resolve these issues.


Solution

  • If i connect the device with system after device locks , the cpu keeps running. So device wakes up when i call unlock_screen() method. If i disconnect the device , after it goes to lock mode, the cpu doesn't run.So device doesn't wakes up always when i call unlock_screen() method. So I have Used the PARTIAL_WAKE_LOCK in order to run the Cpu.