Example usage for android.os CountDownTimer cancel

List of usage examples for android.os CountDownTimer cancel

Introduction

In this page you can find the example usage for android.os CountDownTimer cancel.

Prototype

public synchronized final void cancel() 

Source Link

Document

Cancel the countdown.

Usage

From source file:com.coinblesk.client.MainActivity.java

private void initSwitch(Menu menu) {
    MenuItem item = menu.findItem(R.id.myswitch);
    View view = item.getActionView();
    final Switch mySwitch = (Switch) view.findViewById(R.id.switchAB);
    final AtomicReference<CountDownTimer> ref = new AtomicReference<>();
    mySwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override//from  w  ww . ja  v  a  2 s.c o m
        public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
            if (isChecked) {
                //enable BT
                ref.set(new CountDownTimer(30000, 1000) {
                    int i = 0;

                    public void onTick(final long millisUntilFinished) {
                        mySwitch.setButtonDrawable(
                                (i++ % 2) == 0 ? R.drawable.bluetooth_onon : R.drawable.bluetooth_on);
                        mySwitch.setTextOn("" + millisUntilFinished / 1000);
                    }

                    public void onFinish() {
                        mySwitch.setButtonDrawable(R.drawable.bluetooth_on);
                        mySwitch.setChecked(false);
                    }

                });
                ref.get().start();
                LocalBroadcastManager.getInstance(MainActivity.this)
                        .sendBroadcast(new Intent(Constants.START_CLIENTS_ACTION));

            } else {
                //mySwitch.setShowText(false);
                CountDownTimer tmp;
                if ((tmp = ref.getAndSet(null)) != null) {
                    tmp.cancel();
                }
                LocalBroadcastManager.getInstance(MainActivity.this)
                        .sendBroadcast(new Intent(Constants.STOP_CLIENTS_ACTION));
            }
        }
    });
}

From source file:io.lhyz.android.zhihu.daily.ui.main.LatestAdapter.java

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    final int type = getItemViewType(holder.getAdapterPosition());
    if (type == TYPE_TOP) {
        final List<Top> topStories = mTopStories;
        final CarouselViewHolder carouselViewHolder = (CarouselViewHolder) holder;
        final ViewPager viewPager = carouselViewHolder.mViewPager;
        final TopAdapter adapter = new TopAdapter(topStories);
        adapter.setOnStoryItemClickListener(mOnStoryItemClickListener);
        viewPager.setOffscreenPageLimit(5);
        viewPager.setAdapter(adapter);/*from  ww  w. j a v a2  s. c  o m*/

        /**
         * ??3s?
         *
         * ?
         */
        final CountDownTimer timer = new CountDownTimer(3000, 3000) {
            @Override
            public void onTick(long l) {

            }

            @Override
            public void onFinish() {
                int size = mTopStories.size();
                int pos = viewPager.getCurrentItem();
                if (pos == size - 1) {
                    pos = 0;
                } else {
                    pos += 1;
                }
                viewPager.setCurrentItem(pos);
            }
        };
        timer.start();
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {
                //?
                if (state == ViewPager.SCROLL_STATE_DRAGGING) {
                    timer.cancel();
                }
                if (state == ViewPager.SCROLL_STATE_SETTLING) {
                    timer.start();
                }
            }
        });

    } else if (type == TYPE_HEADER) {
        final HeaderViewHolder headerViewHolder = (HeaderViewHolder) holder;
        final Normal story = mStories.get(0);
        headerViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mOnStoryItemClickListener.onNormalClick(story);
            }
        });
        headerViewHolder.mHeader.setText("");
        headerViewHolder.mTextView.setText(story.getTitle());
        final String imageURL = story.getImages().get(0);
        if (imageURL != null) {
            headerViewHolder.mSimpleDraweeView.setImageURI(Uri.parse(imageURL));
        }
    } else {
        final NormalViewHolder normalViewHolder = (NormalViewHolder) holder;
        final Normal story = mStories.get(holder.getAdapterPosition() - 1);
        normalViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mOnStoryItemClickListener.onNormalClick(story);
            }
        });
        normalViewHolder.mTextView.setText(story.getTitle());
        final String imageURL = story.getImages().get(0);
        if (imageURL != null) {
            normalViewHolder.mSimpleDraweeView.setImageURI(Uri.parse(imageURL));
        }
    }
}

From source file:org.messic.android.smartphone.activities.searchmessicservice.SearchMessicServiceActivity.java

public void onSearchServiceClick(View view) {
    this.buttonSearch.setEnabled(false);

    final CountDownTimer cdt = new CountDownTimer(15000, 1000) {
        @Override//w  w w  .  j av  a 2s.c o  m
        public void onTick(long millisUntilFinished) {

            buttonSearch.setText(getString(R.string.searchMessicService_countdown_searching) + " ("
                    + (millisUntilFinished / 1000) + getString(R.string.searchMessicService_countdown_seconds)
                    + ")");
        }

        @Override
        public void onFinish() {
            buttonSearch.post(new Runnable() {
                public void run() {
                    buttonSearch.setEnabled(true);
                    buttonSearch.setText(R.string.searchMessicService_searchaction);
                }
            });
            presenter.cancelSearch();
        }
    };
    cdt.start();

    presenter.searchMessicServices(new MessicDiscovering.SearchListener() {
        public boolean messicServiceFound(final MDMMessicServerInstance md) {
            // let's see if the instance was found already
            if (!rvListAdapter.existInstance(md)) {
                SearchMessicServiceActivity.this.runOnUiThread(new Runnable() {
                    public void run() {
                        lempty.setVisibility(View.GONE);
                        if (rvListAdapter.addInstance(md)) {
                            rvListAdapter.notifyDataSetChanged();
                        }
                    }
                });

                cdt.cancel();
                cdt.onFinish();
                return true;
            }
            return false;
        }
    });
}