Android学习笔记之——Timer中schedule()的用法

Android学习笔记之——Timer中schedule()的用法

schedule的意思(时间表、进度表)

一、四种标准形式

有四种标准形式(在API的java.util.Timer中可以找到):

(1)schedule(TimerTask task, Date when, long period)Schedule a task for repeated fixed-delay execution after a specific time hasbeen reached

(2)schedule(TimerTask task, long delay, long period)

Schedule a task forrepeated fixed-delay execution after a specific delay.

(3)schedule(TimerTask task, Date when)

Schedule a task forsingle execution.

(4)schedule(TimerTask task, long delay)

Schedule a task forsingle execution after a specified delay.

二、常用形式及其参数说明:

timer.schedule(new TimerTask(){ void run()},long delay, 60*60*1000);

参数说明:

第一个参数,是保护构造函数TimerTask 类,在包:import java.util.TimerTask .使用者要继承该类,并实现public void run() 方法,因为 TimerTask 类 实现了 Runnable 接口。

第二个参数的意思是,当你调用该方法后,该方法必然会调用 TimerTask 类 TimerTask 类 中的 run()方法,这个参数就是这两者之间的差值,转换成汉语的意思就是说,用户调用schedule() 方法后,要等待这么长的时间才可以第一次执行run() 方法。

第三个参数的意思就是,第一次调用之后,从第二次开始每隔多长的时间调用一次 run() 方法。(注意:其单位是毫秒,1000为一秒,60*1000为一分钟,60*60*1000为一小时)

三、使用举例

红绿黄三色闪烁:

//MainActivity.java

package com.example.six;

import java.util.Timer;

import java.util.TimerTask;

import android.R.integer;

import android.os.Bundle;

import android.os.Handler;

import android.app.Activity;

importandroid.text.format.Time;

import android.view.Menu;

import android.widget.TextView;

 

public class MainActivityextends Activity {

   private int currentColor = 0;

   private TextView view;

   final int[] colors = new int[] { R.color.color1, R.color.color2,

        R.color.color3 };

   Handler handler = new Handler() {

      public void handleMessage(android.os.Message msg) {

        if (msg.what == 1) {

           view.setBackgroundResource(colors[currentColor %colors.length]);

        }

        currentColor++;

        super.handleMessage(msg);

      }

   };

 

   @Override

   protected void onCreate(Bundle savedInstanceState) {

      super.onCreate(savedInstanceState);

      setContentView(R.layout.activity_main);

      view = (TextView) findViewById(R.id.view01);

      new Timer().schedule(new TimerTask() {

 

        @Override

        public void run() {

           // TODO 自动生成的方法存根

           handler.sendEmptyMessage(1);

// sendMessage()允许你处理Message对象(Message里可以包含数据)

//sendEmptyMessage()只能放数据

        }

      }, 0, 800);

   }

}

//Activity_main.xml

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical">

 

    <TextView

        android:id="@+id/view01"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_gravity="center"

        android:background="#f00"

        android:height="120px"

        android:width="120px"/>

</LinearLayout>

//Colors.xml

<?xmlversion="1.0"encoding="utf-8"?>

<resources>

   <colorname="color1">#f00</color>

   <colorname="color2">#0f0</color>

   <colorname="color3">#ff0</color>

</resources>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值