Android上每30秒自动生成随机数

时间:2013-03-12 08:54:08

标签: java android random

当我点击生成按钮时,我希望我的程序每30秒自动生成一个随机数,问题是如何在我的代码中执行此操作?

public class MainActivity extends Activity {

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

        final Random myRandom = new Random();


        Button buttonGenerate = (Button)findViewById(R.id.generateme);
        final TextView textGenerateNumber = (TextView)findViewById(R.id.generatenumber);

        buttonGenerate.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {
                ArrayList<Object> Arry1 = new ArrayList<Object>();
                for(int i=0;i<1;i++){
                    ArrayList<Integer> Arry = new ArrayList<Integer>();
                    for(int k=0;k<7;k++){
                        Arry.add(myRandom.nextInt(10));
                    }
                    Arry1.add(Arry);
                }
                textGenerateNumber.setText(String.valueOf(Arry1));
            }
        });  
    }
}

1 个答案:

答案 0 :(得分:0)

设置一个Timer并为TimerTask提供自己的扩展。当你点击按钮时激活计时器,你应该很好。

http://enos.itcollege.ee/~jpoial/docs/tutorial/essential/threads/timer.html

http://docs.oracle.com/javase/6/docs/api/java/util/Timer.html

以下是基于您的代码的示例,以帮助您入门。请注意,由于活动生命周期,它会崩溃:

package com.example.tmp;

import java.util.ArrayList;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;


public class MainActivity extends Activity {
    Timer timeoutTimer;
    final Random myRandom = new Random();
    GenerateTask genTask = new GenerateTask();
    final ArrayList<Object> arry1 = new ArrayList<Object>();

    class GenerateTask extends TimerTask {
        boolean started = false;
        @Override
        public void run() {
            if (started) {
                System.out.println("generating");
                final TextView textGenerateNumber = (TextView)findViewById(R.id.generatenumber);
                arry1.clear();
                for(int i=0;i<1;i++){
                    ArrayList<Integer> Arry = new ArrayList<Integer>();
                    for(int k=0;k<7;k++){
                        Arry.add(myRandom.nextInt(10));
                    }
                    arry1.add(Arry);
                }
                runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        textGenerateNumber.setText(String.valueOf(arry1));
                    }
                });
            }
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);        
        Button buttonGenerate = (Button)findViewById(R.id.generateme);

        buttonGenerate.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v) {
                System.out.println("click");
                if (!genTask.started) {
                    genTask.started=true;
                    timeoutTimer = new Timer();
                    timeoutTimer.scheduleAtFixedRate(genTask, 0, 30000);
                } else {
                    genTask.started=false;
                    timeoutTimer.cancel();
                }
            }
        });  
    }
}