我在onresume方法中调用了handler方法,它是重复的任务,每20秒重复一次,然后当应用程序进入后台时,我调用处理程序方法removecallbacks(null)来阻止它。但它不能正常工作... < / p>
@Override
protected void onPause() {
super.onPause();
handler.removeCallbacks(null);
}
@Override
protected void onResume() {
super.onResume();
//new AsynchHttpDownload(true,this, "GetReviewsNew", 2, ReviewsActivity.this).execute();
customHandler = new android.os.Handler();
// customHandler.notify();
isRunAlways=true;
runAnimation=true;
if((runThread==null)||(runThread!=null&&!runThread.isAlive())){
runThread=new Thread(thread);
runThread.start();
}
pre_empList = new ArrayList<HashMap<String, String>>();
TimTask();
}
public void TimTask()
{
customHandler.postDelayed(updateTimerThread,20000);
//
}
public Runnable updateTimerThread = new Runnable()
{
public void run()
{
Log.d("Repeat Tag","Repeat after 20 sec");
// spin.setVisibility((View.VISIBLE));
new GetSizeFromUpdateReviews().execute();
customHandler.postDelayed(this, 20000);
}
};
答案 0 :(得分:2)
试
handler.removeCallbacks(updateTimerThread);
而不是
handler.removeCallbacks(null);