Android 使用线程更新UI(附带计时器demo)
2015-03-13 23:19
309 查看
上一篇写到两种使用线程简单实用的方法,那么使用线程更新UI,只要使用得当还是可以的。虽然说更新UI需要主线程,请看下面解说:用线程更新UI。
下载地址:http://download.csdn.net/download/qq_16064871/8499931
转载请注明出处:http://blog.csdn.net/qq_16064871
代码上有注释,看代码吧。
TimeManage类
顺便贴上xml文件:
欢迎指正讨论。
附带这上写的计时器项目:
下载地址:http://download.csdn.net/download/qq_16064871/8499931
转载请注明出处:http://blog.csdn.net/qq_16064871
下载地址:http://download.csdn.net/download/qq_16064871/8499931
转载请注明出处:http://blog.csdn.net/qq_16064871
package com.example.thanderdemo; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.annotation.SuppressLint; import android.app.Activity; import android.view.View; import android.widget.TextView; import android.widget.Toast; @SuppressLint("HandlerLeak") public class MainActivity extends Activity { private TimeManage mTimeManage; // 计时管理类 private TextView mtvTime; private int mntime = -1; // 更新界面的线程 private Handler mHandlerUI = new Handler() { public void handleMessage(Message msg) { Bundle dataBundle = msg.getData(); if (dataBundle == null) return; mntime = dataBundle.getInt("time"); if (mntime >= 0) mtvTime.setText(String.valueOf(mntime)); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mtvTime = (TextView) findViewById(R.id.tv_time); mTimeManage = new TimeManage(mHandlerUI); } // 按键开始监听 public void Start(View v) { if (mntime > 0) { Toast.makeText(getApplicationContext(), "已经开始计时....", Toast.LENGTH_LONG).show(); } else { mTimeManage.StaetTime(); } } // 按键结束监听 public void End(View v) { mTimeManage.EndTime(); } }
代码上有注释,看代码吧。
TimeManage类
package com.example.thanderdemo; import android.os.Bundle; import android.os.Handler; import android.os.Message; public class TimeManage { // handler句柄 Handler mHandler; private int i = 0; public TimeManage(Handler handler) { mHandler = handler; handler.post(updateThread); // 启动线程 } Handler handler = new Handler(); // 将要执行的操作写在线程对象的run方法当中 Runnable updateThread = new Runnable() { public void run() { //停留1秒 handler.postDelayed(updateThread, 1000); OnReceiveTime(i); i++; } }; // 开始计时 public void StaetTime() { if (i == 0) { handler.post(updateThread); // 启动线程 } } // 结束计时 public void EndTime() { handler.removeCallbacks(updateThread); i = 0; OnReceiveTime(i); } // 用句柄hander返回数据时,有时需要封装一下数据。 // 用Bundle封装。不能用msg.obj直接返回数据。更新。 public void OnReceiveTime(int ntime) { Message msg = new Message(); Bundle dataBundle = new Bundle(); dataBundle.putInt("time", ntime); msg.setData(dataBundle); mHandler.sendMessage(msg); } }
顺便贴上xml文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LinearLayout2" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity" > <LinearLayout android:layout_width="match_parent" android:layout_height="100dp" android:gravity="center" > <TextView android:id="@+id/tv_time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="66sp" android:text="0" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:gravity="center" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="Start" android:text="开始" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="End" android:text="结束" /> </LinearLayout> </LinearLayout>
欢迎指正讨论。
附带这上写的计时器项目:
下载地址:http://download.csdn.net/download/qq_16064871/8499931
转载请注明出处:http://blog.csdn.net/qq_16064871
相关文章推荐
- Android的线程使用来更新UI----Thread、Handler、Looper、TimerTask等
- Android的线程使用来更新UI----Thread、Handler、Looper、TimerTask等
- Android的线程使用来更新UI----Thread、Handler、Looper、TimerTask等
- Android的线程使用来更新UI----Thread、Handler、Looper、TimerTask等
- Android的线程使用来更新UI----Thread、Handler、Looper、TimerTask等
- Android的线程使用来更新UI----Thread、Handler、Looper、TimerTask等
- Android的线程使用来更新UI----Thread、Handler、Looper、TimerTask等
- Android的线程使用来更新UI----Thread、Handler、Looper、TimerTask等
- Android的线程使用来更新UI----Thread、Handler、Looper、TimerTask等
- Android的线程使用来更新UI----Thread、Handler、Looper、TimerTask等
- Android的线程使用来更新UI----Thread、Handler、Looper、TimerTask等
- Android的线程使用来更新UI----Thread、Handler、Looper、TimerTask等
- Android的线程使用来更新UI----Thread、Handler、Looper、TimerTask等
- Android的线程使用来更新UI----Thread、Handler、Looper、TimerTask等
- Android的线程使用来更新UI----Thread、Handler、Looper、TimerTask等
- Android的线程使用来更新UI----Thread、Handler、Looper、TimerTask等!!!!!!!!!!
- Android的线程使用来更新UI----Thread、Handler、Looper、TimerTask等
- Android的线程使用来更新UI----Thread、Handler、Looper、TimerTask等
- Android的线程使用来更新UI----Thread、Handler、Looper、Time...
- Android的线程使用来更新UI----Thread、Handler、Looper、TimerTask等