Android onTaskRemoved()调用Web服务 [英] Android onTaskRemoved() call to webservice

查看:69
本文介绍了Android onTaskRemoved()调用Web服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

早上好,我的处境很糟糕.我正在会话中创建位置共享逻辑.我将该会话保留在mysql上的服务器上.当android触发该活动时,我会相应地插入用户信息.当android离开课程活动时我删除了该列,因此会话在另一侧被放弃.一切都很好,直到出现一个问题为止.Android不会为最近被刷过的应用程序提供回调,正在运行的应用程序被完全杀死.我在附近找到了一个工作.我正在使用服务并在达到期望的活动后立即启动服务.在服务中,我有一个简单的东西叫做onTaskRemoved(),它会在应用程序从最近记录中被刷死后立即通知我.一切都很好,直到我要调用的位置为止到我的服务器以删除该列.调用不仅会打断,我在那里永远不会收到任何响应,而是在onDestroy()中一切正常.实际上这是代码

Good day.I have horrible situation.I am creating an location share logic within session.I keep that session on server on the mysql.When android hits on that activity,i insert accordingly user information.When android leaves the activity ofcourse i delete that column so the session is abandoned for opposite side.All is great until one issue.Android does not provide an callback for app being swiped from recents,meanging app was killed completelly.I have found an work around there.I am using an service and starting service as soon as it reaches my desired activity.In service i have simple thing called onTaskRemoved() which notifies me as soon as the app was killed by swiping it from recents.All good until the point where i want to call to my server in order to remove the column.The call will not just get through,i will never receive any response there,but in onDestroy() everything working as expected.Actually here is the code

 @Override
public void onTaskRemoved(Intent rootIntent) {
    destroySession();
    super.onTaskRemoved(rootIntent);
}

private void destroySession() {
    Log.d("Fsafasfsafasfas", "destroySession: " + opponentId + " my user id" + sharedHelper.getUserId());
    Call<ResponseBody> locationCall = Retrofit.getInstance().getInkService().requestFriendLocation(sharedHelper.getUserId(), opponentId, "", "", Constants.LOCATION_REQUEST_TYPE_DELETE);
    locationCall.enqueue(new Callback<ResponseBody>() {
        @Override
        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
            if (response == null) {
                destroySession();
                return;
            }
            if (response.body() == null) {
                destroySession();
                return;
            }
            try {
                String responseBody = response.body().string();
                Log.d("Fsafasfsafasfas", "onResponse: " + responseBody);
                JSONObject jsonObject = new JSONObject(responseBody);
                boolean success = jsonObject.optBoolean("success");
                if (success) {
                    stopSelf();
                } else {
                    destroySession();
                }
            } catch (IOException e) {
                stopSelf();
                e.printStackTrace();
            } catch (JSONException e) {
                stopSelf();
                e.printStackTrace();
            }
        }

        @Override
        public void onFailure(Call<ResponseBody> call, Throwable t) {
            destroySession();
        }
    });
}

我猜电话永远不会打通,因为它是唯一打印的日志,是ID的日志,仅此而已..任何人都知道这是怎么回事?我该如何处理这种情况?

The call will never get through i guess as the only log which is printed,is the id's logs and nothing more..Anyone have a clue whats going on?and how can i handle this situation?

推荐答案

我通过取消线程策略的严格模式解决了这一问题.因此可以从主线程完成http请求.

I solved this by canceling the strict mode of the thread policy. So http requests can be done from the main thread.

 StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
 StrictMode.setThreadPolicy(policy);

在正常情况下不建议这样做,但是当应用程序崩溃或关闭时,我认为可以发送一个http请求.

This is not recommended in regular situation, but when the app crashes or closed i guess its ok for sending one http request.

这篇关于Android onTaskRemoved()调用Web服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆