Android定時器Demo

這幾天一直忙簡歷和麪試。都沒敲代碼。

面試四家,都沒消息。

週五,估計沒面試電話,還是靜下來敲代碼吧。

練習下定時器,要不要說想寫個天氣預報。。。

好吧,看看能不能堅持下來。加油,騷年。

準備使用服務,定時器請求天氣數據,以前只是聽,從來沒寫過定時器代碼,看看網上demo 一次搞定。

註釋感覺還可以,貼代碼:

  1 package com.wangyyworks.weatherworks;
  2 
  3 import java.util.Timer;
  4 import java.util.TimerTask;
  5 
  6 import android.os.Bundle;
  7 import android.os.Handler;
  8 import android.os.Message;
  9 import android.app.Activity;
 10 import android.content.res.ColorStateList;
 11 import android.graphics.Color;
 12 import android.view.Menu;
 13 import android.view.View;
 14 import android.widget.TextView;
 15 import android.widget.Toast;
 16 
 17 /**
 18  * 2013-6-7 13:36:59 
 19  * 定時器練習
 20  * @author wang_xiaohao
 21  *
 22  */
 23 public class HomeActivity extends Activity {
 24 
 25     private Handler handler;// 創建Handler;
 26     public final int TIMETASKDEME = 1;// Message 爲1時候 更新UI 文字
 27     private TextView cheangetext;
 28     private int time = 1;
 29     private boolean isTimeStart;//添加標記,方式多次開啓定時器,時間會亂。。。。
 30 
 31     @Override
 32     protected void onCreate(Bundle savedInstanceState) {
 33         super.onCreate(savedInstanceState);
 34         setContentView(R.layout.activity_home);
 35 
 36         inteId();
 37 
 38         /**
 39          * Handler 更新UI
 40          */
 41         handler = new Handler() {
 42 
 43             @Override
 44             public void handleMessage(Message msg) {
 45                 // TODO Auto-generated method stub
 46                 super.handleMessage(msg);
 47                 switch (msg.what) {
 48                 case 1:
 49                     cheangetext.setTextColor(Color.RED);
 50                     cheangetext.setText("每隔一秒更新一次,這是第" + time++ + "秒了~");
 51                     break;
 52                 }
 53                 super.handleMessage(msg);
 54             }
 55 
 56         };
 57 
 58     };
 59 
 60     /**
 61      * 加載ID findviewbyid
 62      */
 63     private void inteId() {
 64         // TODO Auto-generated method stub
 65 
 66         cheangetext = (TextView) findViewById(R.id.cheangetext);
 67 
 68     }
 69 
 70     @Override
 71     public boolean onCreateOptionsMenu(Menu menu) {
 72         // Inflate the menu; this adds items to the action bar if it is present.
 73         getMenuInflater().inflate(R.menu.activity_home, menu);
 74         return true;
 75     }
 76 
 77     /**
 78      * 2013-6-7 13:00:0 測試定時器,服務後期定時請求數據
 79      * 
 80      * @param view
 81      */
 82     public void timetask(View view) {
 83         
 84         
 85         
 86         if (!isTimeStart) {
 87             Toast.makeText(getApplicationContext(), "開啓定時器每一秒執行一次", 0).show();
 88             
 89             // 創建定時器
 90             TimerTask task = new TimerTask() {
 91 
 92                 @Override
 93                 public void run() {
 94                     // TODO Auto-generated method stub
 95                     Message message = new Message();
 96                     message.what = 1;
 97                     handler.sendMessage(message);
 98                 }
 99             };
100 
101             Timer timer = new Timer(true);
102             timer.schedule(task, 1000, 1000); // 延時1000ms後執行,1000ms執行一次
103         }
104         
105         isTimeStart = true;//修改標記爲true,防止第二次開啓;
106         
107     }
108 
109 }

第一次出現問題,多次點擊後數字亂了。。。

添加了標記:isTimeStart搞定。

佈局一個button 一個textview

<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".HomeActivity" >
    
    <Button
        android:onClick="timetask"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="測試定時器" />
    
    <TextView
        android:id="@+id/cheangetext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="40sp"
        android:text="顯示文字" />

</LinearLayout>

 

接下來,該請求數據了。。。

順帶make下 miui 截圖位置:I:\MIUI\screen_cap

加油,不然媳婦都娶不來,騷年。

截個圖:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章