Simple record, countdown alertdialog

Source: Internet
Author: User

A simple alertdialog that displays the countdown.

Dialog display and start countdown. When the countdown ends, alertdialog disappears or alertdialog disappears to cancel the timer.

Do not rely on activity to display dialog, and the permission is required:

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>

Alertdialog creation:

Public void createdialog () {If (malertdialog! = NULL) {return;} layoutinflater Inflater = layoutinflater. From (mcontext); view v = Inflater. Inflate (R. layout. dialog_timertask, null); If (V! = NULL) {mtextview = (textview) v. findviewbyid (R. id. dialog_text);} Builder = new alertdialog. builder (mcontext); builder. settitle ("Countdown:"); builder. setview (V); builder. setnegativebutton ("cancel", null); builder. setpositivebutton ("sure", new onclicklistener () {@ overridepublic void onclick (dialoginterface dialog, int which) {}}); malertdialog = builder. create (); malertdialog. getwindow (). settype (Window Manager. layoutparams. type_system_alert); // display malertdialog. listener (New ondismisslistener () {@ overridepublic void ondismiss (dialoginterface DIALOG) {malertdialog = NULL; If (response! = NULL) {mcountdowntimer. Cancel (); mcountdowntimer = NULL ;}}});}

Countdown:

    private void CountDown(){    if (mCountDownTimer!=null) {return;}        mCountDownTimer = new CountDownTimer(8000,1000) {@Overridepublic void onTick(long millisUntilFinished) {mTextView.setText(millisUntilFinished/1000+"s");}@Overridepublic void onFinish() {if (mAlertDialog!=null) {mAlertDialog.dismiss();}}};mCountDownTimer.start();    }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.