0

時間が現在の時間と等しくなるタイミングをスキャンし、タイマーを使用したいだけですが、チェックボックスをクリックして現在の時間と同じになると...致命的なエラーが発生します。他の解決策はありますか?

        privae int currentHourOfDay,currentMinutes;
        private int mHourOfDay, mMinutes;
        .....

        chk1 = (CheckBox) findViewById(R.id.checkBox1);
        chk1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if (chk1.isChecked()) {

                h = tv.getText().toString(); //get text from hours input
                m = tv2.getText().toString(); //get text from miutes input
                mHourOfDay = Integer.parseInt(h);
                mMinutes = Integer.parseInt(m);

                System.out.println(mHourOfDay);
                System.out.println(mMinutes);

                final Timer timer = new Timer();

                timer.schedule( new TimerTask() {
                    public void run() { 

                Calendar cal = Calendar.getInstance();
                currentHourOfDay = cal.get(Calendar.HOUR_OF_DAY);
                currentMinutes = cal.get(Calendar.MINUTE);

                System.out.println(currentHourOfDay);
                System.out.println(currentMinutes);

                Integer x = mHourOfDay;
                Integer y = currentHourOfDay;
                Integer x1 = mMinutes;
                Integer y1 = currentMinutes;

                if (x.equals(y) && x1.equals(y1)) {

                    Toast toast = Toast.makeText(getApplicationContext(),
                            "Now it equels!!", Toast.LENGTH_SHORT);
                    toast.show();
                    timer.cancel();

                } 
            }}, 0, 1000);


            } else {
                System.out.println("alarm is off");
            }
        }
    });

Mb 私は自分のタスクを解決するために他の方法を使用する必要があります。私の時間が現在と等しいときにイベントを実行しますか?

4

1 に答える 1

0

UI スレッドで ui を更新します。ui スレッドにトースト メッセージを表示します。

トーストを表示する場所に以下を追加するだけです。

      runOnUiThread(new Runnable() //update ui here
         {
          public void run() 
          {
         Toast.makeText(getApplicationContext(),
                        "Now it equels!!", Toast.LENGTH_SHORT).show();
          }
         });

タイマータスクの例

_t.scheduleAtFixedRate( new TimerTask() {
    @Override
    public void run() {
         //do something
        runOnUiThread(new Runnable() //update ui here
         {
          public void run() 
          {
         //display toast here.
          }
         });
    }
}, 1000, 1000 );
于 2013-03-19T10:41:14.147 に答える