你的位置:首页 > 操作系统

[操作系统]安卓定时器


如图是效果图

其中2个文本文件是获取当前的系统时间

其中最关键的是计时器的代码设置

如下是计时器的代码和布局文件

 1 package org. 2  3 import java.util.Timer; 4 import java.util.TimerTask; 5  6 import ogg.huanxin.huadong.R; 7 import android.content.Context; 8 import android.os.Handler; 9 import android.util.AttributeSet; 10 import android.view.LayoutInflater; 11 import android.view.View; 12 import android.widget.LinearLayout; 13 import android.widget.TextView; 14 import android.widget.Toast; 15  16 public class TimeView extends LinearLayout { 17   // 小时的十位数 18   private TextView tv_hour_decade; 19   // 小时的个位数 20   private TextView tv_hour_unit; 21   // 分钟的十位数 22   private TextView tv_min_decade; 23   // 分钟的个位数 24   private TextView tv_min_unit; 25   // 秒的十位数 26   private TextView tv_sec_decade; 27   // 秒的个位数 28   private TextView tv_sec_unit; 29  30   private Context context; 31   private int hour_decade; 32   private int hour_unit; 33   private int min_decade; 34   private int min_unit; 35   private int sec_decade; 36   private int sec_unit; 37  38   // 计时器 39   private Timer timer; 40   private Handler handler = new Handler() { 41     public void handleMessage(android.os.Message msg) { 42       countdown(); 43     } 44  45   }; 46  47   public TimeView(Context context, AttributeSet attrs) { 48     super(context, attrs); 49     // TODO Auto-generated constructor stub 50     this.context = context; 51  52     LayoutInflater inflater = (LayoutInflater) context 53         .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 54     View view = inflater.inflate(R.layout.jishidemo, this); 55     tv_hour_decade = (TextView) view.findViewById(R.id.tv_hour_decade); 56     tv_hour_unit = (TextView) view.findViewById(R.id.tv_hour_unit); 57     tv_min_decade = (TextView) view.findViewById(R.id.tv_min_decade); 58     tv_min_unit = (TextView) view.findViewById(R.id.tv_min_unit); 59     tv_sec_decade = (TextView) view.findViewById(R.id.tv_sec_decade); 60     tv_sec_unit = (TextView) view.findViewById(R.id.tv_sec_unit); 61  62   } 63  64   /** 65    * 开始倒计时 66   */ 67   public void start() { 68     if (timer == null) { 69       timer = new Timer(); 70       timer.schedule(new TimerTask() { 71  72         @Override 73         public void run() { 74           // TODO Auto-generated method stub 75           handler.sendEmptyMessage(0); 76         } 77       }, 0, 1000); 78     } 79   } 80  81   /** 82    * 结束计时 83   */ 84   public void stop() { 85     if (timer != null) { 86       timer.cancel(); 87       timer = null; 88     } 89   } 90  91   /** 92    * 设置时间的参数 93   */ 94   public void setTime(int hour, int min, int sec) { 95     if (hour >= 60 || min >= 60 || sec >= 60 || hour < 0 || min < 0 96         || sec < 0) { 97       throw new RuntimeException("Time format is error"); 98     } 99 100     hour_decade = hour / 10;101     hour_unit = hour - hour_decade * 10;102 103     min_decade = min / 10;104     min_unit = min - min_decade * 10;105 106     sec_decade = sec / 10;107     sec_unit = sec - sec_decade * 10;108     tv_hour_decade.setText(hour_decade + "");109     tv_hour_unit.setText(hour_unit + "");110     tv_min_decade.setText(min_decade + "");111     tv_min_unit.setText(min_unit + "");112     tv_sec_decade.setText(sec_decade + "");113     tv_sec_unit.setText(sec_unit + "");114 115   }116 117   /**118    * 变化时间的十位数119    * 120    * @param tv121    * @return boolean122   */123   private boolean isCarry4Decade(TextView tv) {124     int time1 = Integer.valueOf(tv.getText().toString());125     time1 = time1 - 1;126     if (time1 < 0) {127       time1 = 5;128       tv.setText(time1 + "");129       return true;130     } else {131       tv.setText(time1 + "");132       return false;133     }134   }135 136   private boolean isCarry4Unit(TextView tv) {137 138     int time = Integer.valueOf(tv.getText().toString());139     time = time - 1;140     if (time < 0) {141       time = 9;142       tv.setText(time + "");143       return true;144     } else {145       tv.setText(time + "");146       return false;147     }148   }149 150   /**151    * 倒计时152   */153   private void countdown() {154     if (isCarry4Unit(tv_sec_unit)) {155       if (isCarry4Decade(tv_sec_decade)) {156 157         if (isCarry4Unit(tv_min_unit)) {158           if (isCarry4Decade(tv_min_decade)) {159 160             if (isCarry4Unit(tv_hour_unit)) {161               if (isCarry4Decade(tv_hour_decade)) {162                 Toast.makeText(context, "时间到了",163                     Toast.LENGTH_SHORT).show();164                 stop();165               }166             }167           }168         }169       }170     }171   }172 }

布局文件

<?  android:layout_width="match_parent"  android:layout_height="wrap_content"  android:paddingLeft="10dp"  android:paddingRight="10dp"  android:background="#FCC0E9"  android:orientation="horizontal" >  <TextView    android:id="@+id/tv_hour_decade"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:layout_weight="1"    android:gravity="center"    android:text="0"    android:textColor="@android:color/white"    android:textSize="15sp"    android:textStyle="bold" />  <TextView    android:id="@+id/tv_hour_unit"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:layout_marginLeft="1dp"    android:layout_weight="1"    android:gravity="center"    android:text="0"    android:textColor="@android:color/white"    android:textSize="15sp"    android:textStyle="bold" />  <TextView    android:layout_width="match_parent"    android:layout_height="match_parent"    android:layout_weight="1"    android:gravity="center_horizontal|bottom"    android:text="时"    android:textColor="#ffffff"    android:textSize="13sp" />  <!-- -->  <TextView    android:id="@+id/tv_min_decade"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:layout_weight="1"    android:gravity="center"    android:text="0"    android:textColor="@android:color/white"    android:textSize="15sp"    android:textStyle="bold" />  <TextView    android:id="@+id/tv_min_unit"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:layout_marginLeft="1dp"    android:layout_weight="1"    android:gravity="center"    android:text="0"    android:textColor="@android:color/white"    android:textSize="15sp"    android:textStyle="bold" />  <TextView    android:layout_width="match_parent"    android:layout_height="match_parent"    android:layout_weight="1"    android:gravity="center_horizontal|bottom"    android:text="分"    android:textColor="#ffffff"    android:textSize="13sp" />  <!-- -->  <TextView    android:id="@+id/tv_sec_decade"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:layout_weight="1"    android:gravity="center"    android:text="0"    android:textColor="@android:color/white"    android:textSize="15sp"    android:textStyle="bold" />  <TextView    android:id="@+id/tv_sec_unit"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:layout_marginLeft="1dp"    android:layout_weight="1"    android:gravity="center"    android:text="0"    android:textColor="@android:color/white"    android:textSize="15sp"    android:textStyle="bold" />  <TextView    android:layout_width="match_parent"    android:layout_height="match_parent"    android:layout_weight="1"    android:gravity="center_horizontal|bottom"    android:text="秒"    android:textColor="#ffffff"    android:textSize="13sp" /></LinearLayout>

View Code

其中主代码文件

 1 package org. 2  3 import java.text.SimpleDateFormat; 4 import java.util.Calendar; 5 import java.util.Date; 6  7 import ogg.huanxin.huadong.R; 8 import android.app.Activity; 9 import android.os.Bundle;10 import android.widget.TextView;11 import android.widget.Toast;12 13 public class JiShi extends Activity {14   private TimeView timeView;15   private TextView tv_timeTextView;16   private TextView tView;17 18   @Override19   protected void onCreate(Bundle savedInstanceState) {20     // TODO Auto-generated method stub21     super.onCreate(savedInstanceState);22     super.setContentView(R.layout.main_jishi);23     timeView = (TimeView) super.findViewById(R.id.tv_myjishi);24     tv_timeTextView = (TextView) super.findViewById(R.id.tv_shijian);25     tView = (TextView) super.findViewById(R.id.tv_dangqianshijian);26     // 设置时间27     timeView.setTime(10, 0, 0);28     // 计时开始29     timeView.start();30 31     // 获取当前系统时间32     Calendar calendar = Calendar.getInstance();33     int year = calendar.get(Calendar.YEAR);// 获取年份34     int month = calendar.get(Calendar.MONTH) + 1;// 获取月份35     // int day = calendar.get(Calendar.DAY_OF_MONTH);//当前日期36     int day = calendar.get(Calendar.DATE);// 当前日期37     int hour = calendar.get(Calendar.HOUR_OF_DAY);// 当期时间38     int min = Calendar.getInstance().get(Calendar.MINUTE);// 当前分钟39     int sec = calendar.get(Calendar.SECOND);// 当前秒钟40 41     String s = "当前时间为:" + year + "年" + month + "月" + day + "日" + hour + "时"42         + min + "分" + sec + "秒";43 44     tv_timeTextView.setText(s);45     Toast.makeText(this, s, Toast.LENGTH_LONG).show();46 47     SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");48     String string = df.format(new Date());49     tView.setText(string);50 51   }52 }

View Code
 1 <? 2 <LinearLayout  3   android:layout_width="match_parent" 4   android:layout_height="match_parent" 5   android:background="#eee" 6   android:orientation="vertical" > 7  8   <TextView 9     android:layout_width="wrap_content"10     android:layout_height="wrap_content"11     android:paddingLeft="10dp"12     android:text="距商品开枪还有"13     android:textColor="#030303"14     android:textSize="20sp" />15 16   <View17     android:layout_width="match_parent"18     android:layout_height="1dp"19     android:background="@android:color/white" />20 21   <org.22     android:id="@+id/tv_myjishi"23     android:layout_width="wrap_content"24     android:layout_height="wrap_content"25     android:paddingLeft="10dp" >26   </org.27 28   <TextView29     android:id="@+id/tv_shijian"30     android:layout_marginTop="10dp"31     android:layout_width="match_parent"32     android:layout_height="wrap_content" />33 34   <TextView35     android:id="@+id/tv_dangqianshijian"36     android:layout_marginTop="10dp"37     android:layout_width="match_parent"38     android:layout_height="wrap_content" />39 40 </LinearLayout>