android 当应用程序处于onPause()状态时,我不断收到LogCat上的错误消息

vlju58qv  于 2022-12-28  发布在  Android
关注(0)|答案(2)|浏览(156)

现在我有了下面的类,为了从Internet检索一些数据,我在主Activity中示例化了该类。
如您所见,该类包含一个AsyncTask,它执行从Internet的数据收集。
当我的活动暂停时(通过模拟器上的BACK按钮),我停止AsyncTask。
然而,LogCat继续显示消息,因为AsyncTask仍在继续尝试从Internet获取数据。
这是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;      
        }
    }
}
jq6vz3qz

jq6vz3qz1#

找到了谜底:参见I continue to receive messages after Activity is destroyed
特勤局的人真的被杀了。
问题是,该服务包含一个无限循环,即使该服务被取消/销毁,循环仍在继续。

rekjcdws

rekjcdws2#

我觉得下面这条线里面的问题

client.getConnectionManager().shutdown();

您将找到有关您的解决方案的更多描述
这是原始的Apache文档,也可以帮助更好地理解。

相关问题