【问题标题】:How to rerun a task如何重新运行任务
【发布时间】:2014-09-28 09:37:39
【问题描述】:

我有一个任务正在运行,如果它失败了,我想再次运行它最多 3 次。问题是 Android 会因为我试图在它自己内部运行一个任务而生气。解决此问题的最佳方法是什么?

代码(查看底部task.callback):

final GenericAsyncTask task = new GenericAsyncTask();

        task.background = new Runnable() {
            public void run() {
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost(GCMIntentService.this.host);
                String addremove = "add";
                if(register == false) addremove = "remove";

                try {
                    // Add your data
                    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(4);
                    nameValuePairs.add(new BasicNameValuePair("cmd", addremove));
                    nameValuePairs.add(new BasicNameValuePair("subscription_key", SUBSCRIPTION_KEY)); // unique per app
                    nameValuePairs.add(new BasicNameValuePair("token", str));
                    nameValuePairs.add(new BasicNameValuePair("os_family", "android"));
                    if(addremove.equals("remove")) nameValuePairs.add(new BasicNameValuePair("hard", "" + hard));
                    UrlEncodedFormEntity entity = new UrlEncodedFormEntity(nameValuePairs);
                    httppost.setEntity(entity);
                    Log.i(LCHApplication.TAG, "Name Value Pairs: " + nameValuePairs.toString());

                    // Execute HTTP Post Request
                    HttpResponse response = httpclient.execute(httppost);
                    task.result = EntityUtils.toString(response.getEntity());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        };

        task.callback = new Runnable() {
            public void run() {
                try {
                    Log.i(LCHApplication.TAG, "registration response: " + task.result.toString());
                } catch(Exception e) {
                    if(attempts++ < ATTEMPTS_LIMIT) {
                        // try to register the device again
                        task.execute();
                    } else {
                        Crashlytics.logException(e);
                    }
                }
            }
        };

        task.execute();

【问题讨论】:

    标签: java android android-asynctask illegalstateexception


    【解决方案1】:

    问题是 Android 生气了,因为我试图在它自己内部运行一个任务

    除此之外,AsyncTask 是一次性对象。你不能重新execute() 一个AsyncTask 实例。

    解决这个问题的最佳方法是什么?

    创建AsyncTask 的新实例。例如,您可以实现一个复制构造函数,您可以使用该构造函数从先前执行的实例中克隆一个新实例。

    或者,不要使用AsyncTask

    无论GenericAsyncTask 是什么,我都不能说,因为Android SDK 中没有GenericAsyncTask

    【讨论】:

      【解决方案2】:

      在实际任务中重试,不要重新运行。如果您需要重新运行任务,请通过第 3 方执行。

      【讨论】:

        【解决方案3】:

        在这种情况下我建议的方法是采用处理程序,每次捕获异常时,您都会发布一条执行消息(最多 3 次)。我发现它更灵活,特别是当您需要频繁运行时某些任务。

         private final Handler handler = new Handler() {
        
             public void handleMessage(Message msg) {
        
                   if(msg.what == YOUR_MESSAGE_TYPE){
                    try {
                        Log.i(LCHApplication.TAG, "registration response: " + task.result.toString());
                    } catch(Exception e) {
                        if(attempts++ < ATTEMPTS_LIMIT) {
                            // try to register the device again
                            handler.sendEmptyMsg(YOUR_MESSAGE_TYPE);
                        } else {
                            Crashlytics.logException(e);
                        }
                    }
                }    
             }
        
        };
        

        最后,您必须将其与后台 HandlerThread 相关联。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2023-03-31
          • 1970-01-01
          • 1970-01-01
          • 2016-06-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多