实现功能:某控件不操作10秒后,自动消失。如照相机变焦条出现后,无操作10秒自动隐藏。
所用知识:handler message
//定义变量
private EffectInVisiableHandler mtimeHandler;
private final int MOBILE_QUERY = 1;
//程序启动时,初始化并发送消息
mtimeHandler = new EffectInVisiableHandler();
Message msg = mtimeHandler.obtainMessage(MOBILE_QUERY);
mtimeHandler.sendMessageDelayed(msg, 10000);
//在某控件的onclick或ontouch事件中,重置message,即从新计时开始。
View.OnTouchListener touchCenterLayoutListener=new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction())
{
case MotionEvent.ACTION_DOWN:
resetTime();
break;
case MotionEvent.ACTION_UP:
break;
}
return true;
}
};
private class EffectInVisiableHandler extends Handler
{
@Override
public void handleMessage(Message msg)
{
switch(msg.what)
{
case MOBILE_QUERY:
midLayoutInVisable(); //当10秒到达后,作相应的操作。
Log.i("","run**********************");
break;
}
}
}
public void resetTime() {
mtimeHandler.removeMessages(MOBILE_QUERY);
Message msg = mtimeHandler.obtainMessage(MOBILE_QUERY);
mtimeHandler.sendMessageDelayed(msg, 10000);
}
Android 启动画面 SplashScreen
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
public class SplashActivity extends Activity {
private final int SPLASH_DISPLAY_LENGHT = 5000; //延迟五秒
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
new Handler().postDelayed(new Runnable() {
// 为了减少代码使用匿名Handler创建一个延时的调用
public void run() {
Intent i = new Intent(SplashActivity.this, MainActivity.class);
// 通过Intent打开最终真正的主界面Main这个Activity
SplashActivity.this.startActivity(i); // 启动Main界面
SplashActivity.this.finish(); // 关闭自己这个开场屏
}
}, SPLASH_DISPLAY_LENGHT);
}
}
标签:MOBILE,mtimeHandler,Handler,自动,msg,new,Message,public
From: https://blog.51cto.com/u_5454003/6174596