-2

I'm a newbie to this field of android development. I'm developing an app which I need to be launched by shaking the device. How can I get this thing done? I read many threads and tried out several codes. but non of them worked. Please be kind enough to come up with the full code (from top to bottom) of particular file(or files). So that I'll be able to understand where exactly that I need to change in my code. Thank you!

  • 1
    what have you done? post some code here – Divyesh Patel Apr 27 '17 at 11:13
  • 1
    If I did it, I'd implement a service, which was working all the time on the device, launched by BOOT_COMPLETE, and in the service I'd listen to accelometer data, determine the *shake*. Then just starting Activity when it is determined – Vladyslav Matviienko Apr 27 '17 at 11:15
  • "I read many threads and tried out several codes. but non of them worked" -- then provide a [mcve] demonstrating exactly what you tried and explain exactly what did not work. "Please be kind enough to come up with the full code (from top to bottom) of particular file(or files)" -- that is not how Stack Overflow works. If you want custom code written for you, hire somebody. – CommonsWare Apr 27 '17 at 11:39
  • @CommonsWare I'm sorry You have misunderstood me completely. As I said earlier I'm a newbie to this field of Android developement. There are times when people come up with answers saying different phrases. For example if u take a look at the first answer below, It says that "Now use onSensorChanged to get the reading". My knowledge is not good enough to understand where EXACTLY I need put this thing in my code. Whether I should place it in MainActivity or ShakeService? If it's MainActivity, then where exactly in it? Inside of OnCreate() method or after that? I dont get any of those. Im stil – Sankha Rathnayake Apr 27 '17 at 11:54
  • "My knowledge is not good enough to understand where EXACTLY I need put this thing in my code" -- that is fine. However, the proper way to handle this is to post a comment on a question, asking for clarification, if and when an answer gets posted that you do not completely understand. – CommonsWare Apr 27 '17 at 11:56
  • @CommonsWare I'm sorry You have misunderstood me completely. As I said earlier I'm a newbie to this field of Android developement. There are times when people come up with answers saying different phrases. For example if u take a look at the first answer below, It says that "Now use onSensorChanged to get the reading". My knowledge is not good enough to understand where EXACTLY I need put this thing in my code. Whether I should place it in MainActivity or ShakeService? If it's MainActivity, Where exactly in main activity?. Im still learning all these. That's why i'm asking for the full code – Sankha Rathnayake Apr 27 '17 at 11:57
  • "That's why I'm asking to come up with the full code" -- that is not our job. That is your job. Or, you can hire somebody to "write the full code". We are volunteers, here to answer questions. If this project is too complicated for you, start with some other project, and return to this one in the future. – CommonsWare Apr 27 '17 at 11:59

1 Answers1

2

Try this, First create your Service

public class ShakeService extends Service implements SensorEventListener {

private SensorManager sensorMgr;
private Sensor acc;
private long lastUpdate = -1;
private float x, y, z;
private float last_x, last_y, last_z;
private static final int SHAKE_THRESHOLD = 1100;



@Override
public void onCreate() {


    Toast.makeText(this,
            "Service Started", Toast.LENGTH_SHORT).show();
    sensorMgr = (SensorManager) getSystemService(SENSOR_SERVICE);
    acc=sensorMgr.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

    boolean accelSupported=  sensorMgr.registerListener((SensorEventListener) this, acc, SensorManager.SENSOR_DELAY_GAME);
    long curTime11 = System.currentTimeMillis();

    if (!accelSupported) {
        // on accelerometer on this device
        sensorMgr.unregisterListener((SensorEventListener) this,acc);
    }

    super.onCreate();
}


protected void onPause() {
    if (sensorMgr != null) {
        sensorMgr.unregisterListener((SensorEventListener) this,acc);
        sensorMgr = null;
    }
    return;
}


@Override
public int onStartCommand(Intent intent, int flags, int startId) {


    return super.onStartCommand(intent, flags, startId);
}

@Override
public void onDestroy() {

    if (sensorMgr != null) {
        sensorMgr.unregisterListener((SensorEventListener) this,acc);
        sensorMgr = null;
    }
    stopSelf();
    super.onDestroy();
}

@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onSensorChanged(SensorEvent sensorEvent) {

    if (sensorEvent.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
        long curTime = System.currentTimeMillis();
        // only allow one update every 100ms.
        if ((curTime - lastUpdate) > 100) {
            long diffTime = (curTime - lastUpdate);
            lastUpdate = curTime;

            x = sensorEvent.values[SensorManager.DATA_X];
            y = sensorEvent.values[SensorManager.DATA_Y];
            z = sensorEvent.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 myIntent= new Intent(this, MyActivity.class);
                      myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    /                 startActivity(myIntent);
                ////Here start your activity and your application will be started
            }
            last_x = x;
            last_y = y;
            last_z = z;
        }

    }

}
@Override
public void onAccuracyChanged(Sensor sensor, int i) {

 }

}

And make sure to declare your service in manifest

<service
        android:name=".ShakeService"
        android:enabled="true"
        android:exported="true"></service>

Now in your activity start service as

startService(new Intent(MainActivity.this,ShakeService.class));
Abdul Kawee
  • 2,687
  • 1
  • 14
  • 26