Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了android – 停止定期运行AsyncTask大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_874_2@
有关我的修改后的解决方案,请参阅下面

目标@H_675_7@

>定期轮询URL(例如每30秒),但仅在活动位于前台
>如果活动不在前台,则停止轮询@H_675_7@

定期执行@H_675_7@

> Handler对象通过postDelayed方法接收Runnable对象
>在Runnable对象的run方法中启动AsyncTask
>在AsyncTask的onPostExecute中,再次调用Handler对象的postDelayed
>在活动的onResume中,调用Handler对象的post方法
>在活动的onPause中,调用Handler对象的removeCallBACks以删除消息队列中Runnable的待定帖子@H_675_7@

取消投票的问题@H_675_7@

>即使我在onPause中删除了Runnable的待处理帖子,仍然会发生执行其doInBACkground方法的当前运行的AsyncTask在其onPostExecuteis启动时将新的Runnable添加到队列中(基本上在onPause中调用removeCallBACks之后不久)@H_675_7@

我现在如何解决它@H_675_7@

>布尔成员变量shouldPoll已添加到活动中
>在onResume中设置为true,在onPause中设置为false
>在AsyncTask的onPostExecute中,我检查shouldPoll是否为true并且仅在那种情况下调用Handler对象的postDelayed@H_675_7@

关注@H_675_7@

>使用的是shouldPoll变量吗?
>我有点担心在极少数情况下是否有事情不会发生在活动中(因此也就是shouldPoll变量);因此,以某种方式打破了AsyncTask的onPostExecute的逻辑@H_675_7@

代码片段@H_675_7@

主要活动@H_675_7@

Boolean shouldPoll = false;

@Override
protected void onResume() {
    super.onResume();
    shouldPoll = true;
    handler.post(pollURLRunnablE);
}

@Override
protected void onPause() {
    shouldPoll = false;
    handler.removeCallBACks(pollURLRunnablE);
    super.onPause();
}

final Handler handler = new Handler();

final Runnable pollURLRunnable = new Runnable() {
    public void run() {
        PollingAsyncTask polltimestampAsyncTask = new PollingAsyncTask();
        polltimestampAsyncTask.execute();
    }
};

的AsyncTask@H_675_7@

@Override
protected void onPostExecute(Result result) {
    if (result != null) {
        //Do something here
    }
    if (shouldPoll) {
        handler.postDelayed(pollURLRunnable,10000);
    }
}

更新@H_675_7@

主要活动@H_675_7@

@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);
    }
};

Pollingservice@H_675_7@

@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);
}
@H_874_2@

解决方法

您可以通过在后台线程中运行hander来跳过AsyncTask.然后将工作移动到您发布到处理程序的runnable.

HandlerThread handlerThread = new HandlerThread("BACkground thread");
    handlerThread.start();
    handler = new Handler(handlerThread.getLooper());
@H_874_2@ @H_874_2@
@H_874_2@
@H_874_2@

大佬总结

以上是大佬教程为你收集整理的android – 停止定期运行AsyncTask全部内容,希望文章能够帮你解决android – 停止定期运行AsyncTask所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。