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.
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:
B. Create a shake event listener class:
C. Boot receiver implementation - register a shake listener for TYPE_ACCELEROMETER events
D. If Shake motion is detected - start your main activity:
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: