我们正在尝试在android中实现摇动方法。如果我们关闭应用程序,它会使用服务在后台运行,在这种情况下,它会在后台运行,但不会使用传感器和加速度计达到摇动方法。任何人都可以提供该服务的示例代码. 请指导我们并告诉它是否可能
@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);
}