0

ゲームで作業しているので、毎秒更新される時間を使用する必要があります。TimerTaskを使用しています。ボタンをクリックしたときに時間を一時停止し、他のアクティビティから再開ボタンをクリックしたときに再開したい。それを行う方法は私を助けてください。

t=new Timer();
{
t.scheduleAtFixedRate(new TimerTask()
{
public void run()
{
runOnUiThread(new Runnable() {

@Override
public void run() 
{
TextView tv = (TextView) findViewById(R.id.time);
tv.setText(String.format("%02d:%02d",minute,seconds));

time += 1;
seconds += 1;
if(seconds==60)
{
seconds=0;
}
minute=time/60;
}

                    });
                }
            }, 0, 1000);

        }
4

3 に答える 3

0

まず第一に、CountDownTimerを使用する方がはるかに優れています。CountDownTimerは内部でハンドラーを使用し、UI(メインスレッド)でコールバックを実行します。これにより、コードをよりクリーンにすることができます。

CountDownTimer timer = new CountDownTimer(1000000, 1000) {

    @Override
    public void onTick(long millisUntilFinished) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onFinish() {
        // TODO Auto-generated method stub

    }
};
timer.start();

次に、あるアクティビティから別のアクティビティに参照を渡す必要があります。最も一般的で単純で醜い方法は、静的リゾルバークラスを持つことだと思います。

public class CountDownTimer {
    private static Alarms sCountDownTimer;

    public static CountDownTimer getCountDownTimer () {
        if (sCountDownTimer  == null) throw new NullPointerException("CountDownTimer not initialized yet");
        return sCountDownTimer;
    }

    public static void init(CountDownTimer countDownTimer) {
        if (sCountDownTimer  == null) {
            sCountDownTimer  = countDownTimer;
        } else throw new RuntimeException("Attept to reinitialize!");
    }
}
于 2013-03-24T14:23:11.253 に答える
0
Try this this is what You want
just copy and paste


public class MainActivity extends Activity {
EditText t;
    Button b;
    Button b2;
    Timer timer;
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        b=(Button)findViewById(R.id.button1);
        b2=(Button)findViewById(R.id.button2);
        t=(EditText)findViewById(R.id.editText1);
        b.setBackgroundResource(android.R.color.holo_green_dark);
        timer = new Timer();
        TimerTask updateProfile = new CustomTimerTask(MainActivity.this);
        timer.scheduleAtFixedRate(updateProfile, 0,1000); 
b.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    timer.cancel();
            }
        });
b2.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
                // TODO Auto-generated method stub
     timer = new Timer();
    TimerTask updateProfile = new CustomTimerTask(MainActivity.this);
    timer.scheduleAtFixedRate(updateProfile, 0,1000);
            }
        });
    } 

    class CustomTimerTask extends TimerTask {

        private Context context;
        private Handler mHandler = new Handler();

        // Write Custom Constructor to pass Context
        public CustomTimerTask(Context con) {
            this.context = con;
        }

        @Override
        public void run() {

    new Thread(new Runnable() {
    @Override
    public void run() {
    mHandler.post(new Runnable() {
    @Override
    public void run() {
        Calendar c =Calendar.getInstance(); 
        int seconds = c.get(Calendar.SECOND);
        int min= c.get(Calendar.MINUTE);
        int hour= c.get(Calendar.HOUR);
                                t.setText(hour+":"+min+":"+seconds);
                        }
                    });
                }
            }).start();

        }

    }
}
于 2013-03-24T14:51:46.217 に答える
0

実際には、UIスレッドに投稿する方がはるかに優れており、遅れています。あなたは次のようなものが欲しいです:

final TextView tv = (TextView) findViewById(R.id.time);
getMainLooper().postDelayed(
    new Runnable() {
        @Override void run() {
            tv.setText(String.format("%02d:%02d",minute,seconds));
    } },
    60 * 1000)
于 2013-03-24T14:58:06.697 に答える