When App background shaking method not working

110 views Asked by At

We are trying to implement shaking method in android.If we have close the app it runs background using services,In that case it runs background but it not hitting the shaking method using sensors and accelrometer .Can any one give the sample code for the service. Please guide to us and tell it's possible or not

 @Override
    public void onShake(float force) {

        if (location != null) {

            onLocationChanged(location);
        } else {
            Toast.makeText(getBaseContext(), "No Location found!",
                    Toast.LENGTH_SHORT).show();
        }


        preferences = getApplicationContext().getSharedPreferences("prefs", Context.MODE_PRIVATE);
        reg_email = preferences.getString("Emailid", "");
        phone1 = preferences.getString("Phone1", "");
        phone2 = preferences.getString("Phone2", "");
        phone3 = preferences.getString("Phone3", "");

        // token = preferences.getString("token", "");

        StringRequest stringRequest1 = new StringRequest(Request.Method.POST, NOTIFICATION_EMAIL_URL,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        System.out.println("Response : " + response.toString());

                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                // your code to handle error or failure
                // dismissDialog(DIALOG_LOADING);
                // Toast.makeText(getApplicationContext(), "Failure", Toast.LENGTH_LONG).show();
            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> mapParams = new HashMap<String, String>();
                mapParams.put("EmailID", reg_email);
                mapParams.put("PhoneNumber1", phone1);
                mapParams.put("PhoneNumber2", phone2);
                mapParams.put("PhoneNumber3", phone3);
                mapParams.put("Latitude", String.valueOf((latitude)));
                mapParams.put("Longitude", String.valueOf((longitude)));
                mapParams.put("Address", Address);
                //mapParams.put("TokenID", token);
                return mapParams;
            }
        };
        stringRequest1.setRetryPolicy(new DefaultRetryPolicy(15000,
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        AppController.getInstance().addToRequestQueue(stringRequest1);

    }
1

There are 1 answers

5
Rajen Raiyarela On

Create a class ShakeEventListener

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

  void handleShake(event) {
    if (shake movement detected) {
         //Do your process or event.
    }
  }
}

Inside your service onCreate() register a this listener with sensor manager

public class ShakeHandleService extends Service {
    @Override
    public void onCreate() {
        Log.i(TAG, "Service onCreate");

        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 
    }
}

Make sure you call unregisterListener when starting the activity else your will not receive shake event in your activity.