您的位置:首页 > 移动开发 > Android开发

Android Timer,TimerTask简单的使用

2016-04-10 14:21 483 查看
<pre name="code" class="html"> private void init() {
mTimer = new Timer();
mTimerTask = new TimerTask() {
@Override
public void run() {
//System.out.println("---> count=" + count);
DoTask() ;
//tView.setText(buff.toString()) ;
count++ ;
if (count == -1) {
mTimer.cancel();
}
}

};
//开始一个定时任务
mTimer.schedule(mTimerTask, 2000,4000);
}


</pre><p></p><p> mTimer.schedule(mTimerTask, 2000,4000);中第一个参数是自定义的一个TimerTask对象,对其的run方法实现我们要循环调用的代码。第二个参数是延时多少毫秒开始使用,第三个参数表示,间隔多少毫秒去调用一次。</p><p>完整的一个代码,实现的去调用本地服务器上的一个csv文件。</p><p>java文件</p><p></p><pre code_snippet_id="1641193" snippet_file_name="blog_20160410_7_6655857" name="code" class="java">package com.example.getcsvtest;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class ScvTestActivity extends Activity {

private int count = 0 ;
private Timer mTimer;
private TimerTask mTimerTask;
private TextView tView ;
public StringBuffer buff = new StringBuffer() ;
//private	MyAsyncTask myAsyncTask ;
private Handler handler = new Handler(){
public void handleMessage(Message msg){
tView.setText(buff.toString()+count) ;
}

};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scv_test);

tView = (TextView) findViewById(R.id.TVIEW_ID) ;
init() ;

}

private void init() {
mTimer = new Timer();
mTimerTask = new TimerTask() {
@Override
public void run() {
//System.out.println("---> count=" + count);
DoTask() ;
//tView.setText(buff.toString()) ;
count++ ;
if (count == -1) {
mTimer.cancel();
}
}

};
//开始一个定时任务
mTimer.schedule(mTimerTask, 2000,4000);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.scv_test, menu);
return true;
}

public void DoTask(){
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
try {
URL url =new URL("http://10.0.2.2/1.csv") ;
HttpURLConnection connection =(HttpURLConnection) url.openConnection() ;
connection.setRequestMethod("GET") ;
connection.setConnectTimeout(8000) ;
connection.setReadTimeout(8000) ;
InputStream inStream = connection.getInputStream() ;
BufferedReader reader = new BufferedReader(new InputStreamReader(inStream,"GB2312")) ;
String line = null;
int x = 0 ;
buff.setLength(0);
while((line=reader.readLine())!=null && x <3){
System.out.println(line);
String item[] = line.split(",");
x++ ;
buff.append("时间:"+item[0]+",溶解氧: "+item[1]) ;
buff.append("\n") ;
}
Message msg = new Message() ;
handler.sendMessage(msg) ;
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace() ;
}
}
}).start() ;
}

}
Xml布局文件

<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=".ScvTestActivity" >

<Button
android:id="@+id/BTN_ID"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="点击我"/>
<TextView
android:id="@+id/TVIEW_ID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />

</LinearLayout>


Xml的配置文件要上
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: