【问题标题】:why a asynctask is executed once even if ,i close aplication?为什么即使关闭应用程序也会执行一次异步任务?
【发布时间】:2016-04-05 15:50:58
【问题描述】:

我有一个异步任务来检查互联网访问,但有时它不运行,所以如果能解释一下这种奇怪的行为,我将不胜感激。

这是我的代码:

 if(activeNetworkinfo.getType()==ConnectivityManager.TYPE_WIFI){
                        if(NetworkPrivider()){

                        //this is my asyntask with weird behavior,and always enter here.

                            CheckInternetAccess   ch=new CheckInternetAccess();
                            ch.execute();
                            Log.i("sms", "Esta conetado a wifi");
                        }else{
                            mycallback.QuePaso(MainActivity.LOCALIZACION_APAGADA_WIFI);
                        }

                    }if(activeNetworkinfo.getType()==ConnectivityManager.TYPE_MOBILE){
                    Log.i("sms", "Esta conetado a datos");
                        if(GpsON()){
                            CheckInternetAccess  ch=new CheckInternetAccess();
                            ch.execute();
                        }else{
                            mycallback.QuePaso(MainActivity.LOCALIZACION_APAGADA_GPS);
                        }

                    }

这是我的异步任务:

     public class CheckInternetAccess extends AsyncTask<Void, Void, Boolean> {

        //http://clients3.google.com/generate_204
        @Override
        protected Boolean doInBackground(Void... params) {
            Log.i("sms", "Checando acceso a internet...");
            try {
                HttpURLConnection urlc = (HttpURLConnection)
                        (new URL("http://clients3.google.com/generate_204")
                                .openConnection());
                urlc.setRequestProperty("User-Agent", "Android");
                urlc.setRequestProperty("Connection", "close");
                urlc.setConnectTimeout(1000);
                urlc.connect();
                return (urlc.getResponseCode() == 204 && urlc.getContentLength() == 0);
            }catch (SocketTimeoutException ex){
                Log.e("ChecarInternet: ", "Error esperando", ex);

                cancel(true);
            }catch (ConnectException e) {
                Log.e("ChecarInternet: ", "Sin acceso a internet", e);
                cancel(true);
            } catch (Exception e) {
                Log.e("ChecarInternet: ", "Error checking internet connection", e);
                cancel(true);
            }
            return false;
        }
 @Override
    protected void onPostExecute(Boolean result) {

if(result){
//do someting.
}
}
}

当我安装应用程序时执行异步任务,但如果我关闭应用程序并再次打开已经不执行异步任务,直到那之前,我删除应用程序数据或再次安装。 有时会正常执行。

【问题讨论】:

  • 这在你的 onCreate 方法中吗?当您说关闭时,您的意思是您杀死了该应用程序还是它只是在屏幕外?你可能想在 onResume() 中调用它。
  • 在oncreate方法中调用一个类,这个类有我在activitymain中实现的asyntask和一个接口,所以当asyntask结果为真时,在“//do someting”行中对我的mainactivity执行一个回调。”当我关闭应用程序时,我的意思是彻底摧毁。我为我的英语感到抱歉,但我来自墨西哥..

标签: android task execute


【解决方案1】:

这样用,

class MyActivity {
    CheckInternetAccess ch;

    void someMethod() {
        ch = new CheckInternetAccess();
        ch.execute();
    }

    void onPause() { // Activitys onPause
        if (ch != null) {
            ch.cancel(true);
        }
    }
}

【讨论】:

  • 但是我的 asynctask 它不在活动中,我的 asynctask 它在简单的类上,你认为这也有效吗?
  • 是的。然后找出哪个应用程序正在使用这个异步任务。并在需要时取消。
  • 那么 ch.cancel(); 有什么区别?和取消(真); ,因为我在发生异常错误时取消
  • 它将是取消真。如果没有发生错误,它仍在运行。从活动中发布您的完整代码。
  • 我已经解决了这个问题,我的 asynctask 一直在工作,问题是 doinbackground 在很长一段时间后被调用,所以我找到了这个,executeOnExecutor 并且运行良好。但非常感谢 Sazzad Hossain Khan 花时间帮助我。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-09-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多