0

Im using Accelerometer Sensor to detect movments from the phone, and when it detect a movment it send notification. when it send notification i want to stop the Accelerometer sensor. well i have no idea how to do that. any idea guys? I know that i need to unregister but i dont know how and where i need to unregister it.

main activity code

  public class MainActivity extends Activity implements SensorEventListener, Listen {

private BroadcastReceiver statusReceiver;
Sensor accelerometer;
SensorManager sm;
TextView acceleration;
SendValues sv;

private static final String TAG = "MainActivity";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    sm = (SensorManager) getSystemService(SENSOR_SERVICE);
    accelerometer = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
     sm.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL);
    acceleration = (TextView) findViewById(R.id.sensorTxt);

    statusReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String type = intent.getStringExtra("message");  //get the type of message from MyGcmListenerService 1 - lock or 0 -Unlock
            Log.d(TAG, "TypeMessage: " + type);
            if (type == "1") // 1 == lock
            {
                stopService();
            } else {
                onPause();
            }
        }

    };}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@Override
public void onPause()
{
    super.onPause();
    sm.unregisterListener(this);
}

public void stopService()
{
    sm.unregisterListener(this);
}

@Override
public void onResume()
{
    super.onResume();
    sm.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL);
}

@Override
public void onSensorChanged(SensorEvent event) {
    acceleration.setText("X: " + event.values[0] +
            "\nY: " + event.values[1] +
            "\nZ: " + event.values[2]);

    // get the phone number from the login
    SharedPreferences sh = getSharedPreferences("BikePhone", Context.MODE_PRIVATE);
    String phone = sh.getString(Params.PHONE, null);

    if (event.values[0] >= 5.000 || (event.values[0] <= -5.000) || (event.values[1] >= 5.000) || (event.values[1] <= -5.000)) {

        sv = new SendValues(phone, "1");

        Gson g = new Gson();
        String ans = g.toJson(sv, SendValues.class);
        send(sv);
    }
}

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

}


public void send(SendValues sv) {
    SendThreadCommunication con;
    ServerRequest ser = new ServerRequest();
    ser.setResponse(this);
    Gson gson = new Gson();

    String send = gson.toJson(sv, SendValues.class);

    ser.addParamaters(Params.VALUES, send);
    ser.addServerName(Params.SERVER_URL);
    ser.addServletName(Params.BIKE_TO_USER);
    con = new SendThreadCommunication(ser);
    con.start();
}

@Override
public void good() {
    Toast.makeText(getApplication(), "successful transfer", Toast.LENGTH_LONG).show();
}

@Override
public void notGood() {
    Toast.makeText(getApplication(), "UNsuccssful transfer", Toast.LENGTH_LONG).show();
}

@Override
public void userGcmNotRegistered() {
    Toast.makeText(getApplication(), "There is some problem, please register again to the App", Toast.LENGTH_LONG).show();
    }

 }
Bolandian Eran
  • 211
  • 1
  • 4
  • 21

1 Answers1

0

You need to use unregisterListener (SensorEventListener listener) and do it in the onPause() and onStop(). Looks like you're close.

Take a look at the latest documentation (because some of the info in the answers may be deprecated) and these links:

(How to detect shake event with android?)

(Stop Android Accelerometer after Shake)

(Stop recording accelerometer data with button click)

Community
  • 1
  • 1
Sevron
  • 64
  • 3