当app是onPause()时,我继续在LogCat上收到错误消息

时间:2012-12-24 09:43:59

标签: android android-asynctask httpclient android-logcat

谢谢你的帮助和圣诞快乐。

确定。

现在我有了以下类,我在我的主要活动中实例化,以便从互联网上检索一些数据。

正如您所看到的,该类包含一个AsyncTask,它执行互联网数据收集。

当我的活动暂停时(通过模拟器上的BACK按钮),我停止了AsyncTask。

然而,由于AsyncTask仍在继续尝试从互联网上获取数据,LogCat会继续显示消息。

这是LogCat,它永远不会停止......

12-24 09:36:48.806: E/grabXML(698): java.lang.IllegalStateException: Manager is shut down.
12-24 09:36:48.816: E/grabXML(698): Exception fetching data
12-24 09:36:48.816: E/grabXML(698): java.lang.IllegalStateException: Manager is shut down.
12-24 09:36:48.836: E/grabXML(698): Exception fetching data
12-24 09:36:48.836: E/grabXML(698): java.lang.IllegalStateException: Manager is shut down.
12-24 09:36:48.846: E/grabXML(698): Exception fetching data
12-24 09:36:48.846: E/grabXML(698): java.lang.IllegalStateException: Manager is shut down.
12-24 09:36:48.856: E/grabXML(698): Exception fetching data
12-24 09:36:48.856: E/grabXML(698): java.lang.IllegalStateException: Manager is shut down.
12-24 09:36:48.966: E/grabXML(698): Exception fetching data
12-24 09:36:48.966: E/grabXML(698): java.lang.IllegalStateException: Manager is shut down.
12-24 09:36:48.996: E/grabXML(698): Exception fetching data
12-24 09:36:48.996: E/grabXML(698): java.lang.IllegalStateException: Manager is shut   down.
12-24 09:36:49.037: E/grabXML(698): Exception fetching data
12-24 09:36:49.037: E/grabXML(698): java.lang.IllegalStateException: Manager is shut down.

这是我的班级

public class Grab{
    public String url;
    public String srt = "cav";
    public boolean flag = false;
    public GrabXml g;
    public Grab (String u){
        url = u;
    } 

    public String vai(){
        g = new GrabXml();
        g.execute();
        Log.e("", srt);
        while(!flag){
            // nothing
        };
        g.cancel(true);
        return srt;
    }

    public void fermaGrab() {
        g.cancel(true); 
    }

    public class GrabXml extends AsyncTask<Void, String, Void>{
        @Override
        protected Void doInBackground(Void... arg0) {
            Log.e("", "arrivo al do in background");
            Log.e("", url);
            DefaultHttpClient client = new DefaultHttpClient();
            HttpGet getMethod = new HttpGet(url);
            try {
                ResponseHandler<String> responseHandler = new BasicResponseHandler();
                srt = client.execute(getMethod, responseHandler);
                Log.e("", "passo per GrabXML");
            }
            catch (Throwable t) {
                android.util.Log.e("grabXML", "Exception fetching data", t);
            }
            flag = true;
            client.getConnectionManager().shutdown();
            return null;
        }

        @Override
        protected void onProgressUpdate(String... item) {
            // nothing
        }

        @Override
        protected void onPostExecute(Void unused) {
            flag = true;      
        }
    }
}

2 个答案:

答案 0 :(得分:1)

我认为下面一行内的问题

       client.getConnectionManager().shutdown();

你会发现更多Description of your Solution

This is the original Apache documentation也有助于更好地理解。

希望它会有所帮助:)

答案 1 :(得分:1)

找到谜语的解决方案:见I continue to receive messages after Activity is destroyed

该服务实际上正在被杀死。

关键是服务包含一个无限循环,即使服务被取消/销毁,循环也会继续。

祝大家圣诞快乐!