后置延时方法 - 机器人机器人、方法

由网友(屁是饭垂死的呐喊)分享简介:我用postedDelayed方法来刷新我的活动,工​​作正常。但问题是,即使我preSS后退按钮postdelayed方法调用回previous活动。//处理程序30000毫秒后延迟活动的茶点mHandler.postDelayed(新的Runnable(){公共无效的run(){dostuff();}},3000...

我用postedDelayed方法来刷新我的活动,工​​作正常。但问题是,即使我preSS后退按钮postdelayed方法调用回previous活动。

//处理程序30000毫秒后延迟活动的茶点

  mHandler.postDelayed(新的Runnable(){
公共无效的run(){
               dostuff();

        }
            },30000);
    }

保护无效dostuff(){
意向意图= getIntent();
完成(); startActivity(意向);
Toast.makeText(getApplicationContext(),刷新,Toast.LENGTH_LONG).show();
}

公共无效onBack pressed(){
        super.onBack pressed();
        完();
        mHandler.removeCallbacks(空);
        }

保护无效的onStop(){
            mHandler.removeCallbacks(空);
        super.onStop();
    }
 

解决方案

您可以使用 removeCallbacks(可运行)的处理程序使用你所调用方法 postDelayed()方法。例如,如果你使用的:

  mHandler.postDelayed(mRunnable,修改时间)
 
5G对机器人来说意味着什么 毫无延迟从不会耽搁

刷新的活动,然后使用

  mHandler.removeCallbacks(mRunnable)
 

在活动的onPause()方法。

I used postedDelayed method to refresh my Activity, which works fine. But the problem is that even when I press the Back button postdelayed method call back the previous activity..

//handler for 30000 milli-secs post delay refreshment of the activity

mHandler.postDelayed(new Runnable() {
public void run() {
               dostuff();

        }
            }, 30000);
    }

protected void dostuff() {
Intent intent = getIntent();
finish();startActivity(intent);
Toast.makeText(getApplicationContext(), "refreshed", Toast.LENGTH_LONG).show();
}

public void onBackPressed() {
        super.onBackPressed();
        finish();
        mHandler.removeCallbacks(null);
        }

protected void onStop() {
            mHandler.removeCallbacks(null);
        super.onStop();
    }

解决方案

You can use removeCallbacks(runnable) method of the handler using which you are calling postDelayed() method. For example, if you used:

mHandler.postDelayed(mRunnable, mTime)

for refreshing the activity, then use

mHandler.removeCallbacks(mRunnable)

in onPause() method of the activity.

阅读全文

相关推荐

最新文章