java – 处理程序或定时器android

前端之家收集整理的这篇文章主要介绍了java – 处理程序或定时器android前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试着每1分钟显示一个msg!不停!我发现例外,一个固定的延迟后显示msg一次!你可以帮忙怎么设置呢?或者如果使用定时器是更好的工作,我需要一个例子!
public class TimertestActivity extends Activity {
    /** Called when the activity is first created. */

      @Override   
      public void onCreate(Bundle icicle) {   
        super.onCreate(icicle);   
        setContentView(R.layout.main);  
        Handler handler = new Handler();
        handler.postDelayed(
            new Runnable() {
                public void run() {
                    afficher();
                }
            },1000L);

      }   

      public void afficher()
      {
          Toast.makeText(getBaseContext(),"test",Toast.LENGTH_SHORT).show();
      }
}

谢谢!

解决方法

尝试这段代码
public class TimertestActivity extends Activity {
    Handler handler = new Handler();
    Runnable runnable = new Runnable() {
        public void run() {
            afficher();
        }
    };

    /** Called when the activity is first created. */

      @Override   
      public void onCreate(Bundle icicle) {   
        super.onCreate(icicle);   
        setContentView(R.layout.main);  
        runnable.run();
      }   

      public void afficher()
      {
          Toast.makeText(getBaseContext(),Toast.LENGTH_SHORT).show();
          handler.postDelayed(runnable,1000);
      }
}
原文链接:https://www.f2er.com/android/124032.html

猜你在找的Android相关文章