Android 停止定期运行异步任务 有关我修改的解决方案,请参见下面的更新部分 目标 定期(例如每30秒)轮询URL,但仅当活动位于前台时 如果活动不在前台,请停止轮询 定期执行 处理程序对象通过postDelayed方法接收可运行对象 在可运行对象的run方法中,启动AsyncTask 在AsyncTask的onPostExecute中,再次调用处理程序对象的postDelayed 在活动的onResume中,调用处理程序对象的post方法 在活动的onPause中,调用处理程序对象的removeCallbacks,以删除消息队列中Runnable的挂起帖子 取消投票的问题 即使我在onPause中删除了Runnable的挂起POST,但在启动onPostExecute时,执行其doInBackground方法的当前运行的AsyncTask仍可能向队列添加新的Runnable(基本上是在onPause中调用removeCallbacks之后的几分钟) 我现在怎么解决它 已将布尔成员变量shoulpoll添加到活动中 在恢复时将其设置为true,在暂停时将其设置为false 在AsyncTask的onPostExecute中,我检查shouldPoll是否为true,并仅在这种情况下调用处理程序对象的postdayed 担心 使用shouldPoll变量可以吗 我有点担心,在极少数情况下,活动(以及shouldPoll变量)是否会发生某些事情;因此,以某种方式破坏了AsyncTask的onPostExecute 源代码片段 主要活动 异步任务 更新 主要活动 轮询服务

Android 停止定期运行异步任务 有关我修改的解决方案,请参见下面的更新部分 目标 定期(例如每30秒)轮询URL,但仅当活动位于前台时 如果活动不在前台,请停止轮询 定期执行 处理程序对象通过postDelayed方法接收可运行对象 在可运行对象的run方法中,启动AsyncTask 在AsyncTask的onPostExecute中,再次调用处理程序对象的postDelayed 在活动的onResume中,调用处理程序对象的post方法 在活动的onPause中,调用处理程序对象的removeCallbacks,以删除消息队列中Runnable的挂起帖子 取消投票的问题 即使我在onPause中删除了Runnable的挂起POST,但在启动onPostExecute时,执行其doInBackground方法的当前运行的AsyncTask仍可能向队列添加新的Runnable(基本上是在onPause中调用removeCallbacks之后的几分钟) 我现在怎么解决它 已将布尔成员变量shoulpoll添加到活动中 在恢复时将其设置为true,在暂停时将其设置为false 在AsyncTask的onPostExecute中,我检查shouldPoll是否为true,并仅在这种情况下调用处理程序对象的postdayed 担心 使用shouldPoll变量可以吗 我有点担心,在极少数情况下,活动(以及shouldPoll变量)是否会发生某些事情;因此,以某种方式破坏了AsyncTask的onPostExecute 源代码片段 主要活动 异步任务 更新 主要活动 轮询服务,android,android-asynctask,Android,Android Asynctask,通过让hander在后台线程中运行,您可以跳过异步任务。然后只需将工作移动到发布到处理程序的可运行状态 HandlerThread handlerThread = new HandlerThread("Background thread"); handlerThread.start(); handler = new Handler(handlerThread.getLooper()); 使用IntentService。保持处理程序不变,只需在需要下载时广播启动服务的意图

通过让hander在后台线程中运行,您可以跳过异步任务。然后只需将工作移动到发布到处理程序的可运行状态

    HandlerThread handlerThread = new HandlerThread("Background thread");
    handlerThread.start();
    handler = new Handler(handlerThread.getLooper());

使用
IntentService
。保持处理程序不变,只需在需要下载时广播启动服务的意图。你还需要使用轮询吗?它消耗电池电量,通常效率低下。考虑使用谷歌云消息或XMPP从你的url推送到手机上。我用solut更新了我的原始帖子ion基于您的建议。不幸的是,我不能使用push,因为URL是由不了解GCM或XMPP的第三方提供的。假设使用IntentService的解决方案不是最有效的,但足够;-)感谢您的建议。最后,我决定使用Rarw建议的IntentService,但您的建议似乎也是可行的选择(至少对我而言)。这很好,有时也让我对Android开发感到沮丧。一、 不是安卓专家,我不能确定我是否从我能想到的多个选项中选择了解决问题的正确方法。在这种情况下,如果在某些罕见的情况下没有出现一些隐藏的问题,我最终会产生怀疑。。。但没有人说这将是一次轻松的旅程;-)欢迎来到精彩的编程世界。您6个月前的代码总是显得古怪和过度冗余。:)
@Override
protected void onPostExecute(Result result) {
    if (result != null) {
        //Do something here
    }
    if (shouldPoll) {
        handler.postDelayed(pollURLRunnable, 10000);
    }
}
@Override
protected void onResume() {
    super.onResume();
    handler.post(startIntentServiceRunnable);
    LocalBroadcastManager.getInstance(this).registerReceiver(statusBroadcastReceiver, new IntentFilter(Constants.MY_INTENT_FILTER));
}

@Override
protected void onPause() {
    handler.removeCallbacks(startIntentServiceRunnable);
    LocalBroadcastManager.getInstance(this).unregisterReceiver(statusBroadcastReceiver);        
    super.onPause();
}

final Handler handler = new Handler();

final Runnable startIntentServiceRunnable = new Runnable() {
    public void run() {
        Intent intent = new Intent(MainActivity.this, PollingService.class);
        startService(intent);
    }
};


final BroadcastReceiver statusBroadcastReceiver = new BroadcastReceiver() {
    public void onReceive(Context context, Intent intent) {
        //...
        //Do something useful with the extras from intent here
        //...
        handler.postDelayed(startIntentServiceRunnable, 2000);
    }
};
@Override
protected void onHandleIntent(Intent intent) {
    //...
    //Perform the polling and prepare results here
    //...
    broadcastResults();

}

private void broadcastResults() {
    Intent intent = new Intent(Constants.MY_INTENT_FILTER);
    //...
    //Fill the intent extras with the data here
    //...
    LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
    HandlerThread handlerThread = new HandlerThread("Background thread");
    handlerThread.start();
    handler = new Handler(handlerThread.getLooper());