Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 264
SHD Avatar asked Jul 02 '14 18:07

SHD


People also ask

How to detect shake event in Android app?

How to detect shake event in Android app? This example demonstrates how to do I in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml. Step 3 − Add the following code to src/MainActivity.java

How to enable full screen activity on lock screen on Android?

To enable it you have to go to the app configuration -> Other permissions -> Show On Lock screen. The full screen activity will also not appear if you have an existing notification with the same id that has not been dismissed.

How to lock apps on an Android device?

To enable screen pinning in Android 8 and 7, go to Settings > Lock screen and security > Other security settings > Pin windows. You can also use third-party apps like Samsung Secure Folder, App Lock, and Norton App Lock to lock your Android apps. This article explains three different ways on how to lock apps on an Android device.

Why is the activity not showing on the lock screen?

It’s not Working !!! For some of you the activity might not appear on the lock screen and that’s because of your phone, not all phones allow full screen intents by default. I have a Xiaomi and it’s not enabled by default. To enable it you have to go to the app configuration -> Other permissions -> Show On Lock screen.


Video Answer


1 Answers

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>
like image 180
Gilad Haimov Avatar answered Nov 08 '22 21:11

Gilad Haimov