目录搜索
androidManifestManifest.permissionManifest.permission_groupandroid.accessibilityserviceAccessibilityServiceandroid.accountsAccountandroid.appNotificationManagerandroid.bluetoothBluetoothAdapterBluetoothClassBluetoothClass.DeviceBluetoothClass.Device.MajorBluetoothClass.ServiceBluetoothDeviceBluetoothServerSocketBluetoothSocketandroid.contentSharedPreferencesandroid.database.sqliteSQLiteCursorDriverSQLiteOpenHelperandroid.graphicsBitmapandroid.locationLocationListenerGeocoderGpsStatusGpsStatus.ListenerGpsStatus.NmeaListenerGpsSatelliteandroid.mediaAudioFormatAsyncPlayerAudioRecordAudioRecord.OnRecordPositionUpdateListenerThumbnailUtilsAudioManagerandroid.netTrafficStatsMailToLocalSocketandroid.osAsyncTaskAsyncTask.StatusCountDownTimerMessageMessageQueueHandlerThreadandroid.textHtmlandroid.utilJsonWriterandroid.viewContextMenuContextMenu.ContextMenuInfoDisplayViewManagerViewViewStubViewTreeObserverViewParentWindowManagerGestureDetectorGravityMenuInflaterScaleGestureDetectorSoundEffectConstantsandroid.view.inputmethodInputConnectionInputMethodInputMethodSessionBaseInputConnectionInputMethodManagerandroid.widgetAbsListViewAbsListView.LayoutParamsAbsListView.OnScrollListenerAbsListView.RecyclerListenerAbsoluteLayoutAbsoluteLayout.LayoutParamsAbsSeekBarAbsSpinnerAdapterViewAdapterView.AdapterContextMenuInfoAdapterView.OnItemLongClickListenerAdapterView.OnItemSelectedListenerAdapterView.OnItemClickListenerAnalogClockBaseAdapterBaseExpandableListAdapterButtonCheckBoxCheckedTextViewCheckableChronometerChronometer.OnChronometerTickListenerCompoundButtonCompoundButton.OnCheckedChangeListenerCursorAdapterCursorTreeAdapterDatePickerDatePicker.OnDateChangedListenerDialerFilterDigitalClockEditTextFilterFilter.FilterListenerFilter.FilterResultsExpandableListAdapterExpandableListView.OnChildClickListenerExpandableListView.OnGroupClickListenerExpandableListView.OnGroupCollapseListenerExpandableListView.OnGroupExpandListenerFilterableGalleryGallery.LayoutParamsGridViewGridLayoutGridLayout.AlignmentRadioGroupImageViewImageView.ScaleTypeHorizontalScrollViewImageButtonImageSwitcherFilterQueryProviderListAdapterListViewMediaControllerMultiAutoCompleteTextViewMultiAutoCompleteTextView.CommaTokenizerMultiAutoCompleteTextView.TokenizerQuickContactBadgeRadioButtonRatingBarRatingBar.OnRatingBarChangeListenerRelativeLayoutRemoteViewsResourceCursorAdapterResourceCursorTreeAdapterScrollerScrollViewSearchViewSearchView.OnCloseListenerSearchView.OnQueryTextListenerSearchView.OnSuggestionListenerSeekBarSeekBar.OnSeekBarChangeListenerSimpleAdapterSimpleAdapter.ViewBinderSimpleCursorAdapterSimpleCursorAdapter.CursorToStringConverterSimpleCursorAdapter.ViewBinderSimpleCursorTreeAdapterSimpleCursorTreeAdapter.ViewBinderSimpleExpandableListAdapterSlidingDrawerSlidingDrawer.OnDrawerCloseListenerSlidingDrawer.OnDrawerOpenListenerSlidingDrawer.OnDrawerScrollListenerSpinnerSpinnerAdapterWrapperListAdapterTabHostTabHost.TabSpecTextViewTimePickerTimePicker.OnTimeChangedListenerToastTableLayoutTableLayout.LayoutParamsTableRowTableRow.LayoutParamsTabWidgetTextSwitcherToggleButtonTwoLineListItemVideoViewViewAnimatorViewFlipperViewSwitcherViewSwitcher.ViewFactoryZoomButtonsControllerZoomButtonsController.OnZoomListenerZoomButtonZoomControlsdalvik.systemDexFile
文字


CountDownTimer

版本:Android 4.0 r1

 

结构

继承关系

public abstract class CountDownTimer extends Object

        

java.lang.Object

android.os.CountDownTimer

 

类概述

定时执行在一段时候后停止的倒计时,在倒计时执行过程中会在固定间隔时间得到通知(译者:触发onTick方法),下面的例子显示在一个文本框中显示一个30s倒计时:

 new CountdownTimer(30000, 1000) {

     public void onTick(long millisUntilFinished) {

         mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);

     }

     public void onFinish() {

         mTextField.setText("done!");

     }

  }.start(); 

onTick的调用是同步的,保证这次调用不会在之前调用完成前发生。这里的同步机制主要是用来:onTick的实现需要很多时间执行比倒计时间隔更重要的事情。

 

构造函数

         public CountDownTimer (long millisInFuture, long countDownInterval)

参数

                   millisInFuture  从开始调用start()到倒计时完成并onFinish()方法被调用的毫秒数。(译者注:倒计时时间,单位毫秒)

                   countDownInterval   接收onTick(long)回调的间隔时间。(译者注:单位毫秒)

 

公共方法

public final void cancel ()

         取消倒计时(译者:取消后,再次启动会重新开始倒计时)        

                  

public abstract void onFinish ()

         倒计时完成时被调用    

 

public abstract void onTick (long millisUntilFinished)

         固定间隔被调用

参数

                            millisUntilFinished   倒计时剩余时间。

 

public synchronized final CountDownTimer start ()

         启动倒计时

 

补充

文章精选

Android 定时器

android倒计时功能的实现(CountDownTimer

示例代码

         Java

package com.test.countdowntimer;

 

import android.app.Activity;

import android.os.Bundle;

import android.os.CountDownTimer;

import android.util.Log;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.TextView;

 

import com.test.R;

 

public class CountDownTimeActivity extends Activity implements OnClickListener {

 

    TextView mTextView;

    Button mButton1;

    Button mButton2;

   

    @Override

    protected void onCreate(Bundle savedInstanceState) {

       super.onCreate(savedInstanceState);

      

       setContentView(R.layout.countdown);

      

        mTextView = (TextView)findViewById(R.id.textView1);

       mButton1 = (Button)findViewById(R.id.button1);

       mButton2 = (Button)findViewById(R.id.button2);

       mButton1.setOnClickListener(this);

       mButton2.setOnClickListener(this);

    }

 

    CountDownTimer timer = new CountDownTimer(40000,1000) {

      

       @Override

       public void onTick(long millisUntilFinished) {

           mTextView.setText("seconds remaining: " + millisUntilFinished / 1000);

           try {

              Thread.sleep(1200);

           } catch (InterruptedException e) {

              e.printStackTrace();

           }

           Log.e("CountDown",millisUntilFinished+"");

       }

      

       @Override

       public void onFinish() {

           mTextView.setText("done");

       }

    };

   

    @Override

    public void onClick(View v) {

       switch(v.getId()){

       case R.id.button1:

           timer.start();

           break;

       case R.id.button2:

           timer.cancel();

           break;

       }

      

    }

}

 

 

         XML

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout

  xmlns:android="http://schemas.android.com/apk/res/android"

  android:orientation="vertical"

  android:layout_width="match_parent"

  android:layout_height="match_parent">

    <TextView android:text="TextView" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>

    <Button android:text="开始" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>

    <Button android:text="取消" android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>

   

</LinearLayout>

 

说明:

CountDownTimer的间隔为1s,我们在onTIck方法里面休眠了1.2s,所以log出来发现打印间隔变成了2s,即中间一次onTick方法没有被执行(不会在之前一次调用完成前被调用)。

 


上一篇:下一篇: