Android App,如何在Activity,Service和SyncAdapter之间进行通信

时间:2016-11-29 18:24:10

标签: android

嗨,我正在开发一个在线和离线工作的Android应用程序。所以如果它连接到wifi,当我点​​击Activity中的save按钮时,会有多个请求(StringRequest)发送到服务器来更新db。如果离线它将数据保存到sqlite,一旦wifi可用,它会自动使用SyncAdapter同步数据。我的问题是有很多冗余/重复的代码 - 一个用于Activity,一个用于SyncAdapter,我试图清理代码。 Activity,Response.Listener&中每个StringRequests的唯一区别。 Response.ErrorListener在Activity中显示一些警报,在SyncAdapter中它只记录消息。 现在我编写了一个可供活动和syncadapter使用的服务类,我的问题是我如何在Response.Listener&中发送警报/更新活动。 Response.ErrorListener,不会破坏服务方法的流程。

private void Method1(){

    if (NetworkConnection.isNetworkAvailable (this))
    {
        StringRequest strRequest = new StringRequest (Request.Method.POST, AppConfigURL.API_URL,
                new Response.Listener<String> () {
                    @Override
                    public void onResponse (String response) {
                        if (response != null) {
                            Utils.showLog (Log.INFO, AppConfigTags.SERVER_RESPONSE, response, true);
                        } else {
                            Utils.showOkDialog (MainActivity.this, "Custom alert message", false);
                        }
                        Method2();
                    }
                },
                new Response.ErrorListener () {
                    @Override
                    public void onErrorResponse (VolleyError error) {
                        Utils.showLog (Log.ERROR, AppConfigTags.VOLLEY_ERROR, error.toString (), true);
                        Method2();
                    }
                }) {
            @Override
            public byte[] getBody () throws com.android.volley.AuthFailureError {

                JSONObject sendJSON = db.getOfflineSavedOrderDetails();

                String str = "{\"API_username\":\"" + Constants.api_username + "\",\n" +
                        "\"API_password\":\"" + Constants.api_password + "\",\n" +
                        "\"API_function\":\"updateSavedFixedDetails\",\n" +
                        "\"API_parameters\": " + String.valueOf(sendJSON) + "}";
                Utils.showLog (Log.INFO, AppConfigTags.PARAMETERS_SENT_TO_THE_SERVER, str, true);
                return str.getBytes ();
            }

            public String getBodyContentType () {
                return "application/json; charset=utf-8";
            }
        };
        AppController.getInstance().addToRequestQueue(strRequest);
    } else {
        Method2();
    }
}

private void Method2 () {

    if (NetworkConnection.isNetworkAvailable (this))
    {
        StringRequest strRequest = new StringRequest (Request.Method.POST, AppConfigURL.API_URL,
                new Response.Listener<String> () {
                    @Override
                    public void onResponse (String response) {
                        if (response != null) {
                            Utils.showLog (Log.INFO, AppConfigTags.SERVER_RESPONSE, response, true);
                        } else {
                            Utils.showLog (Log.WARN, AppConfigTags.SERVER_RESPONSE, AppConfigTags.DIDNT_RECEIVE_ANY_DATA_FROM_SERVER, true);
                        }
                        Method3();
                    }
                },
                new Response.ErrorListener () {
                    @Override
                    public void onErrorResponse (VolleyError error) {
                        Utils.showOkDialog (MainActivity.this, "Connection Error: My custom error", false);
                        Method3();
                    }
                }) {
            @Override
            public byte[] getBody () throws com.android.volley.AuthFailureError {

                JSONObject sendJSON = db.getOfflineSavedOrderDetails();

                String str = "{\"API_username\":\"" + Constants.api_username + "\",\n" +
                        "\"API_password\":\"" + Constants.api_password + "\",\n" +
                        "\"API_function\":\"updateContractDetails\",\n" +
                        "\"API_parameters\": " + String.valueOf(sendJSON) + "}";
                Utils.showLog (Log.INFO, AppConfigTags.PARAMETERS_SENT_TO_THE_SERVER, str, true);
                return str.getBytes ();
            }

            public String getBodyContentType () {
                return "application/json; charset=utf-8";
            }
        };
        AppController.getInstance().addToRequestQueue(strRequest);
    } else {
        Method3();
    }
}

0 个答案:

没有答案