How to create an android app that activates on shake events when screen locked? [closed]
Asked Answered
O

1

7

I want to create an app that **starts the Main activity whenever the device shakes, even when screen locked. Can anyone explain how to do that?

I have an idea that it requires to create a service that runs in background, but I am struggling with actual coding and don't know how to do it.

Oxonian answered 2/7, 2014 at 18:9 Comment(2)
read this : tinyurl.com/so-hintsPohai
Thanks a lot for suggestions. I found a lot of answers in bits and pieces which I couldn't integrate. Hence I needed to ask an open question.Oxonian
M
30

To create an app which is sensitive to shake event:

A. In manifest - register a boot receiver. It will make sure your app will always be activated after device restart:

  <receiver android:name=".OnBootReceiver">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <action android:name="android.intent.action.QUICKBOOT_POWERON" />
        </intent-filter>
    </receiver>



B. Create a shake event listener class:

class ShakeEventListener implements SensorEventListener {
        @Override
        public void onSensorChanged(SensorEvent event) {
              handleShake(event); // see below
        }
}



C. Boot receiver implementation - register a shake listener for TYPE_ACCELEROMETER events

public class OnBootReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent){
        SensorManager sManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
        sensor = sManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        sManager.registerListener(new ShakeEventListener(), sensor, SensorManager.SENSOR_DELAY_NORMAL); // or other delay
    }
}



D. If Shake motion is detected - start your main activity:

void handleShake(event) {
    if (shake movement detected) {
         // start main activity
         Intent intent = new Intent(getBaseContext(), myActivity.class);
         intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
         startActivity(intent);
    }
}



The only thing we left out is the "shake movement detected" logic.

Here you can find a reasonably good base implementation. Use function onSensorChanged(). You will probably need to variate on it until you get it right.



Permissions:

  <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
Mishmash answered 2/7, 2014 at 20:44 Comment(5)
That's a great explanation! Is handleShake(event) a standard method? Android Studio doesn't recognize it. I am unable to connect B. and D. Could you help?Oxonian
if the shake listener is implemented in one activity, we register the listener in onResume and unregister in onPause. What do we do in the service? when do we unregister the listener?Monney
Here I want to know the current activity and context in the ShakeEventListener class. how do I get that?Monney
Brilliant example, but is there any chance somebody can provide an amended part D that would launch an activity of a Cordova/Phonegap app please?Champlain
Maybe this is an old question, but i just want to help. on D change void handleShake(event) { if (shake movement detected) to public void handleShake(SensorEvent event) { if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) this works for me.Gimcrack

© 2022 - 2024 — McMap. All rights reserved.