在CountDownTimer中完成单独的活动与结果

时间:2014-06-06 12:48:30

标签: android android-activity countdowntimer

有没有办法在倒数计时器内为不同的活动调用startActivityForResult()?我目前的代码:

new CountDownTimer(60000, 1000) {

    public void onTick(long millisUntilFinished) {
        long theLong = 0;
        Integer i = (int) (long) theLong;
        i = (int) (Math.round(millisUntilFinished / 1000));  
        tv.append("Waiting..." + "\n");

        switch (i) {
           case 55:
            Intent intentBluetooth = new Intent(MainTestCycle.this, BluetoothActivity.class);
            startActivityForResult(intentBluetooth, Constants.BLUETOOTH_SUBACTIVITY);

            break;
           case 50:
            //Intent intentwifi = new Intent(MainTestCycle.this, WifiActivity.class);
            //startActivityForResult(intentwifi, Constants.WIFI_SUBACTIVITY);

            break;

        }            
    }

    public void onFinish() {

   }

 }.start();

但是没有调用onActivityResult()方法?

有人能指出我正确的方向吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

这段代码有点奇怪(抱歉:)),但我会尝试给你一个解决方案。那么,您打算在活动开始后让倒计时器运行的意图是什么?因为你给了60秒,但是你想要在55秒之后开始。如果此计时器在活动开始后不再执行任何操作,只需等待55秒直到倒计时。我会这样做:

             private class StartActivityTimer extends CountDownTimer {
  public StartActivityTimer(long startTime, long interval) {
   super(startTime, interval);
  }

   @Override
  public void onFinish() {

         Intent intent = new Intent(MainTestCycle.this, BluetoothActivity.class);
         startActivityForResult(intent, Constants.BLUETOOTH_SUBACTIVITY);
  }

  @Override
  public void onTick(long millisUntilFinished) {
   //do nothing
  }
   }

然后,使计时器Global:

      private StartActivityTimer timer = null;

初始化并开始:

      timer = new StartActivityTimer(55000,1000);
      timer.start();