Android定时器和线程实现

来源:互联网 发布:hive sql 平台 编辑:程序博客网 时间:2024/06/05 03:22

在Android开发中,经常会用到需要定时更新界面或者周期性地读取发送数据,那么就涉及到定时器和线程的使用了

定时器就是定时地读取发送数据,其主要与界面相关,例如定时更新数据

线程主要用于处理比较耗时而且与界面无关的操作

下面通过一个例子实现


首先,实现定时器操作

activity_main.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:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context=".MainActivity"    android:orientation="vertical" >    <TextView        android:id="@+id/tv"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/hello_world" />    <Button         android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="second"        android:onClick="btn_second"/></LinearLayout>

MainActivity.java

package com.threaddemo;import java.util.Timer;import java.util.TimerTask;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.annotation.SuppressLint;import android.app.Activity;import android.content.Intent;import android.view.View;import android.widget.TextView;//定时器处理发送数据public class MainActivity extends Activity{//宏定义,表示是什么消息public static final int REFRESH = 0x01;//用来显示变化的值private int nValue = 0;//用来显示值得控件public TextView textView;//定时器设置Timer timer = new Timer();TimerTask task = new TimerTask(){@Overridepublic void run(){nValue++;Message message = new Message();message.what = REFRESH;mhandler.sendMessage(message);}};//处理消息@SuppressLint("HandlerLeak")public Handler mhandler = new Handler(){public void handleMessage(Message msg){switch (msg.what){case REFRESH:RefreshData();break;default:break;}}};public void RefreshData(){textView.setText(String.valueOf(nValue));}@Overrideprotected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);textView = (TextView)findViewById(R.id.tv);nValue = 0;//开启定时器timer.schedule(task,1000,1000);}//跳转到下一个界面public void btn_second(View view){Intent intent = new Intent(MainActivity.this,SecondActivity.class);startActivity(intent);}@Overrideprotected void onDestroy(){// TODO Auto-generated method stubsuper.onDestroy();timer.cancel();timer.purge();}}


效果如图

 


然后,实现线程操作

second.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns: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/tv"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/hello_world" />    <Button         android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="ok"        android:onClick="btn_ok"/></LinearLayout>

SecondActivity.java

package com.threaddemo;import android.annotation.SuppressLint;import android.app.Activity;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.os.SystemClock;import android.view.View;import android.widget.TextView;//线程发送数据public class SecondActivity extends Activity{//宏定义,表示是什么消息public static final int REFRESH = 0x01;//用来显示变化的值private int nValue = 0;//用来显示值得控件public TextView textView;//线程private Thread mThread;//控制线程运行private boolean bIsRunning = false;//线程发送数据Runnable runnable = new Runnable() {        // 重写run()方法,此方法在新的线程中运行        @Override        public void run()         {        while (bIsRunning){        Message message = new Message();    message.what = REFRESH;    message.arg1 = nValue;//发送消息时将值作为参数同时传递    mhandler.sendMessage(message);        nValue++;    SystemClock.sleep(1000);//1000ms的延时}        }    };        //处理消息    @SuppressLint("HandlerLeak")public Handler mhandler = new Handler(){public void handleMessage(Message msg){switch (msg.what){case REFRESH:RefreshData(msg.arg1);break;default:break;}super.handleMessage(msg);}};        public void RefreshData(int value){textView.setText(String.valueOf(value));}@Overrideprotected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.second);textView = (TextView)findViewById(R.id.tv);nValue = 0;bIsRunning = true;//开启线程mThread = new Thread(runnable);mThread.start();}public void btn_ok(View view){SecondActivity.this.finish();}@Overrideprotected void onDestroy(){// TODO Auto-generated method stubsuper.onDestroy();bIsRunning = false;mThread.interrupt();}}

效果如图

 


源码下载


0 0
原创粉丝点击