AsyncTask的中的onDestroy()没有被执行 [英] AsyncTask in onDestroy() not being executed

查看:299
本文介绍了AsyncTask的中的onDestroy()没有被执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要注销信息发送给服务器,当应用程序被破坏了。

其实,一切都确定了注销功能,当用户尝试从活动的菜单中手动注销正常工作。

这里的code:

  @覆盖
保护无效的onDestroy()
{
    尝试
    {
        Log.i(对myApp,活性毁);
        共享preferences preFS1 = getShared preferences(com.my.app,Context.MODE_PRIVATE);
        Log.i(对myApp,步骤1);
        字符串响应;
        Log.i(对myApp,步骤2);
        响应= HttpPOSTer.logout(事件codes.LOGOUT,LOGOUT_EVENT code,prefs.getString(会话ID,空));
        Log.i(对myApp,第三步);
        如果(response.equals(注销))
        {
            Log.i(对myApp,成功地注销);
        }
        其他
        {
            Log.i(对myApp,不能执行注销);
        }
        prefs1.edit()清()提交()。
    }
    赶上(InterruptedException的E)
    {
        e.printStackTrace();
    }
    赶上(为ExecutionException E)
    {
        e.printStackTrace();
    }
    super.onDestroy();
    }
}

但这里的日志,当我关闭从家里按钮的长按菜单中的应用程序:

我甚至无法调试在这里。我把断点,但在调试它永远不会被解雇了。

是否有任何理由,为什么的AsyncTask 不会从的onDestroy被称为()


解决方案

它不是一个好主意,在的onDestroy(使用asyntask),而不是你可以有扩展IntentService一个活动,打个电话,从的onDestroy

I want to send logout information to server, when the app gets destroyed.

Actually, everything is ok with the logout function, it works fine when user tries to logout manually from activity's menu.

Here's the code:

@Override
protected void onDestroy()
{
    try
    {
        Log.i("myApp", "Activity destroyed");
        SharedPreferences prefs1 = getSharedPreferences("com.my.app", Context.MODE_PRIVATE);
        Log.i("myApp", "step1");
        String response;
        Log.i("myApp", "step2");
        response = HttpPOSTer.logout(EventCodes.LOGOUT, LOGOUT_EVENTCODE, prefs.getString("sessionID", "null"));
        Log.i("myApp", "step3");
        if (response.equals("logout"))
        {
            Log.i("myApp", "logged out succesfully");
        }
        else
        {
            Log.i("myApp", "couldn't perform logout");
        }
        prefs1.edit().clear().commit();
    }
    catch (InterruptedException e)
    {
        e.printStackTrace();
    }
    catch (ExecutionException e)
    {
        e.printStackTrace();
    }   
    super.onDestroy();  
    }
}

But here's the log, when I close the app from home button's long click menu:

I can't even debug here. I place breakpoint, but it never gets fired while debugging.

Is there any reason, why AsyncTask doesn't get called from onDestroy()?

解决方案

its not a good idea to use asyntask in onDestroy() instead you can have an activity that extends IntentService and give a call to that activity from onDestroy

这篇关于AsyncTask的中的onDestroy()没有被执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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