后置延时方法 - 机器人 [英] Post Delay Method - Android

查看:201
本文介绍了后置延时方法 - 机器人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用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,修改时间)
 

刷新的活动,然后使用

  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.

这篇关于后置延时方法 - 机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆