1

我们正在尝试在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);

    }
4

1 回答 1

0

创建一个类 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.
    }
  }
}

在您的服务 onCreate() 中向传感器管理器注册一个此侦听器

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

确保在启动活动时调用 unregisterListener 否则您将不会在活动中收到震动事件。

于 2016-02-11T06:20:17.010 回答