0

この場合、タイマーを追加することについて混乱しています。「button_Timer」がクリックされたときに「mService.sendAlert(mDevice, str2)」を毎分送信したい。

public void onClick(View v) {
    switch (v.getId()) {


    case R.id.button_Timer:
        Log.e("MainActivity", "Clicked");
        if (mService != null)

        {
            str2 = Ef.getText().toString();
            str2 = str2.substring(0, 0) + "E" + str2.substring(0, str2.length());
            mService.sendAlert(mDevice, str2);
        }
        break;

    default:
        Log.e(TAG,"wrong Click event");
        break;
    }
}

前もって感謝します

4

2 に答える 2

0

ViewそのためButton、特定の時間の経過後にランナブルをポストして発火させることができる postDelayed() というメソッドがあります。それをランナブルと一緒に使用して、1分に1回のタスクを処理できます。

// Declare this in your activity
Runnable r;

//change your onClick to make and post a recursive runnable.
public void onClick(final View v) { //<-- need to make v final so we can refer to it in the inner class.
    switch (v.getId()) {
    case R.id.button_Timer:
        Log.e("MainActivity", "Clicked");
        r = new Runnable(){
            public void run(){
                if (mService != null){
                    str2 = Ef.getText().toString();
                    str2 = str2.substring(0, 0) + "E" + str2.substring(0, str2.length());
                    mService.sendAlert(mDevice, str2);
                    v.postDelayed(r, 60 * 1000);
                }
            }
        };
        //fire the first run. It'll handle the repeating
        v.post(r);
        break;

    default:
        Log.e(TAG,"wrong Click event");
        break;
    }
}
于 2013-05-09T02:22:28.633 に答える
0
 public void onClick(View v) {
    switch (v.getId()) {


    case R.id.button_Timer:
        Log.e("MainActivity", "Clicked");
        if (mService != null)

        {
            str2 = Ef.getText().toString();
            str2 = str2.substring(0, 0) + "E" + str2.substring(0, str2.length());
            final MyTimer timer = new MyTimer(999999999,60000);
            timer.start();
        }
        break;

    default:
        Log.e(TAG,"wrong Click event");
        break;
    }
}


public class MyTimer extends CountDownTimer{

    public MyTimer(long millisInFuture, long countDownInterval) {
        super(millisInFuture, countDownInterval);
    }

    @Override
    public void onFinish() {
    }

    @Override
    public void onTick(long millisUntilFinished) {
        mService.sendAlert(mDevice, str2);
    }
}
于 2013-05-09T02:23:02.313 に答える