应用程序处于后台/已关闭时,从Android服务进行改造,回调失败

时间:2018-04-26 07:06:43

标签: android android-studio service retrofit retrofit2

我使用Android服务通过融合位置API获取位置,并使用改进API在特定时间间隔内更新位置。

当应用程序打开/前台时,一切正常。但是当应用程序是后台/关闭时,除了改装请求外,所有工作都正常。

使用“无法连接到”消息改进回调失败。当应用程序再次打开时,一切正常。

Retrofit 2回调onResponse on back thread or Services always onFailure

任何帮助都会受到很多关注。感谢

此服务从TimeTask开始:

public class SendLocationService extends IntentService {
private static final String TAG = "SendLocationService";
private Context mContext = null;
private APIInterface apiInterface;

public SendLocationService() {
    super("SendLocationService");
}

@Override
protected void onHandleIntent(@Nullable Intent intent) {
    mContext = getApplicationContext();
    Log.e(TAG, "**************************************");
    Log.e(TAG, "Location Update Time Interval");
    Log.e(TAG, "**************************************");
    Login login = (Login) CommonMethods.retrieveObject(mContext, PreferenceConnector.LOGIN, new Login());
    if (login == null)
        return;
    List<LocationData> locationData = DatabaseHelper.getInstance(mContext).getAllLocation();
    Log.i(TAG, "Time to hit the APIs: " + DatabaseHelper.getInstance(mContext).getAllLocation().size());
    JSONArray jsonArray = new JSONArray();
    List<TrackedLocation> trackedLocations = new ArrayList<TrackedLocation>();
    for (LocationData data : locationData) {
        try {
            jsonArray.put(new JSONObject(data.getData()));
            Gson gson = new Gson();
            TrackedLocation obj = gson.fromJson(data.getData(), TrackedLocation.class);
            trackedLocations.add(obj);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    HashMap<String, Object> hashMap = new HashMap<String, Object>();
    hashMap.put("EmpId", login.getMessage().getID());
    hashMap.put("MapTrack", trackedLocations);

    if (apiInterface == null) {
        apiInterface = APIClient.getClientInstance().create(APIInterface.class);
    }
    apiInterface.postMapTrackingDetails(hashMap).enqueue(new Callback<Message>() {
        @Override
        public void onResponse(Call<Message> call, Response<Message> response) {
            try {
                DatabaseHelper.getInstance(mContext).deleteLocationData();
                PreferenceConnector.resetLocationCounter(mContext);
                response.body();
                Log.e("response:", response.body().getMessage());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onFailure(Call<Message> call, Throwable t) {
            try {
                Log.e("response:", t.getLocalizedMessage());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

}

2 个答案:

答案 0 :(得分:1)

用于改装api调用的用户意图服务以及所有其他操作   BroadCast接收器,以便您可以使用Intent服务在后台调用代码,然后可以使用BroadCast接收器对结果做出反应

答案 1 :(得分:0)

替换以下行:

DatabaseHelper.getInstance(mContext).deleteLocationData();
            PreferenceConnector.resetLocationCounter(mContext);
            response.body();

使用:

if (mContext != null)
{
    DatabaseHelper.getInstance(mContext).deleteLocationData();
    PreferenceConnector.resetLocationCounter(mContext);
    response.body();
}
相关问题