Download Android source code: Verification Code countdown
Function category: Special Effects
Supported platforms: Android
Runtime Environment: Eclipse
Development language: Java
Development tools: Eclipse
Source code size: 1.38 MB
Source code:Http://down.51cto.com/data/1977282
Source code Introduction
Service + CountDownTimer + Handler completes the countdown function, and page switching is not interrupted.
Source code running
Running Effect
Source code snippets
- Package com. zihao. timerdemo;
- Import com. zihao. service. RegisterCodeTimerService;
- Import com. zihao. util. RegisterCodeTimer;
- Import android. OS. Bundle;
- Import android. OS. Handler;
- Import android. OS. Message;
- Import android. view. View;
- Import android. view. View. OnClickListener;
- Import android. widget. Button;
- Import android. annotation. SuppressLint;
- Import android. app. Activity;
- Import android. content. Intent;
- /**
- * Main Interface
- *
- * @ Author zihao
- *
- */
- Public class MainActivity extends Activity {
- Private Button mGetCodeBtn; // countdown Button
- Private Intent mIntent;
- @ Override
- Protected void onCreate (Bundle savedInstanceState ){
- Super. onCreate (savedInstanceState );
- SetContentView (R. layout. activity_main );
- InitView ();
- }
- /**
- * Initialize the view.
- */
- Private void initView (){
- MGetCodeBtn = (Button) findViewById (R. id. get_code_btn );
- RegisterCodeTimerService. setHandler (mCodeHandler );
- MIntent = new Intent (MainActivity. this, RegisterCodeTimerService. class );
- MGetCodeBtn. setOnClickListener (new OnClickListener (){
- @ Override
- Public void onClick (View v ){
- // TODO Auto-generated method stub
- MGetCodeBtn. setEnabled (false );
- StartService (mIntent );
- }
- });
- }
- /**
- * Countdown Handler
- */
- @ SuppressLint ("HandlerLeak ")
- Handler mCodeHandler = new Handler (){
- Public void handleMessage (Message msg ){
- If (msg. what = RegisterCodeTimer. IN_RUNNING) {// countdown in progress
- MGetCodeBtn. setText (msg. obj. toString ());
- } Else if (msg. what = RegisterCodeTimer. END_RUNNING) {// countdown to completion
- MGetCodeBtn. setEnabled (true );
- MGetCodeBtn. setText (msg. obj. toString ());
- }
- };
- };
- @ Override
- Protected void onDestroy (){
- // TODO Auto-generated method stub
- Super. onDestroy ();
- StopService (mIntent );
- }
- }
Source code:Http://down.51cto.com/data/1977282