功能分类:特效
支持平台:Android
运行环境:Eclipse
开发语言:Java
开发工具:Eclipse
源码大小:1.38MB
源码下载地址:http://down.51cto.com/data/1977282
源码简介
Service+CountDownTimer+Handler完成倒计时功能,页面切换不中断。
源码运行截图
运行效果
源码片段
- 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;
- /**
- * 主界面
- *
- * @author zihao
- *
- */
- public class MainActivity extends Activity {
- private Button mGetCodeBtn;// 倒计时按钮
- private Intent mIntent;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- initView();
- }
- /**
- * 初始化视图
- */
- 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);
- }
- });
- }
- /**
- * 倒计时Handler
- */
- @SuppressLint("HandlerLeak")
- Handler mCodeHandler = new Handler() {
- public void handleMessage(Message msg) {
- if (msg.what == RegisterCodeTimer.IN_RUNNING) {// 正在倒计时
- mGetCodeBtn.setText(msg.obj.toString());
- } else if (msg.what == RegisterCodeTimer.END_RUNNING) {// 完成倒计时
- mGetCodeBtn.setEnabled(true);
- mGetCodeBtn.setText(msg.obj.toString());
- }
- };
- };
- @Override
- protected void onDestroy() {
- // TODO Auto-generated method stub
- super.onDestroy();
- stopService(mIntent);
- }
- }