如何在主要活动中循环一段代码?

我正在使用Android Studio,我希望每半秒循环一次

"Random rand = new Random(); int value = rand.nextInt(10);" 

所以无论如何,谢谢你的时间,如果你能帮助那将是伟大的。 🙂

此致
伊戈尔

编辑
感谢大家的善意和有用的答案。 我会在尝试每一个后立即选择最佳答案。 (现在不在我的电脑上)但是再一次,谢谢大家。 编辑
对于任何有类似问题的人,我都可以使用它。 这是最终的代码。 包sarju7.click;

 import android.os.Handler; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.view.View; import android.widget.TextView; import java.util.Random; public class MainActivity extends ActionBarActivity { Random rand = new Random(); Handler handler = new Handler(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Runnable r = new Runnable() { public void run() { int value = rand.nextInt(10); TextView t1 = (TextView) findViewById(R.id.clicker); t1.setText(Integer.toString(value)); handler.postDelayed(this, 400); } }; handler.postDelayed(r, 400); } } 

再次感谢大家。 你们是最棒的。 我喜欢堆栈溢出!

 Random rand = new Random(); Handler handler = new Handler() Runnable r=new Runnable() { public void run() { int value = rand.nextInt(10); handler.postDelayed(this, 500); } }; handler.postDelayed(r, 500); 

使用postDelayed() 。 例如,此活动每五秒显示一次Toast

 /*** Copyright (c) 2012 CommonsWare, LLC Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. From _The Busy Coder's Guide to Android Development_ http://commonsware.com/Android */ package com.commonsware.android.post; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Toast; public class PostDelayedDemo extends Activity implements Runnable { private static final int PERIOD=5000; private View root=null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); root=findViewById(android.R.id.content); } @Override public void onResume() { super.onResume(); run(); } @Override public void onPause() { root.removeCallbacks(this); super.onPause(); } @Override public void run() { Toast.makeText(PostDelayedDemo.this, "Who-hoo!", Toast.LENGTH_SHORT) .show(); root.postDelayed(this, PERIOD); } } 

Runnable run()方法是您完成工作的地方,并安排Runnable在您所需的延迟期后再次运行。 只需调用removeCallbacks()来结束循环。 你可以在任何小部件上调用postDelayed() ; 在我的情况下,我使用框架提供的FrameLayout称为android.R.id.content ,因为此活动没有其他UI。

用这个

在onCreate中创建它

  Random rand = new Random(); handler=new Handler(); handler.postDelayed(myRunnable, 100); 

在onCreate之外声明它

 myRunnable=new Runnable() { @Override public void run() { int value = rand.nextInt(10); handler.postDelayed(this, 100); } } 

虽然这里的所有答案都很好,但它们并不一定能解决您为什么要这样或者您将对结果做什么 。 您可以在代码中的任何位置执行此操作,但您可能会问,因为它会阻止UI线程,如果它在那里运行,您可能不希望这样。

您需要在后台运行此不同的线程或服务,以便用户可以在循环运行时进行交互。 这是基本的答案 – 在MAIN或UI线程之外的另一个线程上运行此循环。 (这里有很多答案可以解决这个问题。)

如果你想要每隔半秒在屏幕上显示一个随机数,那么很多这些选项都没问题,除非他们没有解释如果你在另一个线程中运行它们,那么你需要在Activity创建一个类,然后使用runOnUiThread线程方法更新视图类(否则会出错)。

如果您想将它作为进一步后台处理的开始,您应该考虑一个Service ,您可以在其中扩展循环的function以及在UI线程运行时可能需要执行的任何其他任务。 例如,如果您需要随机数来选择屏幕上显示的图像,则可能需要在为Activity提供图像的服务中运行此选项。

希望有所帮助。