如何让我的应用程序每分钟重复此功能?

时间:2013-04-22 12:11:56

标签: java android function repeat

我正在制作一款应用程序,可以在随机的地方onCreate上显示数字时钟。我希望该应用程序只需每分钟重复该功能。有没有人有任何想法如何实现?

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    TextView textView = (TextView) findViewById(R.id.digitalClock1);  
    Random r = new Random();

    int x = r.nextInt(350 - 100);
    int y = r.nextInt(800 - 100);

    textView.setX(x);  
    textView.setY(y);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

1 个答案:

答案 0 :(得分:1)

使用Handler

Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                Random r = new Random();

                int x = r.nextInt(350 - 100);
                int y = r.nextInt(800 - 100);

                textView.setX(x);  
                textView.setY(y);
            }
        }, 1000 * 60);