編輯:關於Android編程
方法一:Handler+Thread
package com.xunfang.handerDemo; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.widget.TextView; /** * handler定時器 * * @author Smalt * */ public class HanderDemoActivity extends Activity { TextView tvShow; private int i = 0; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); tvShow = (TextView) findViewById(R.id.tv_show); new Thread(new ThreadShow()).start(); } // handler類接收數據 Handler handler = new Handler() { public void handleMessage(Message msg) { if (msg.what == 1) { tvShow.setText(Integer.toString(i++)); System.out.println("receive...."); } }; }; // 線程類 class ThreadShow implements Runnable { @Override public void run() { // TODO Auto-generated method stub while (true) { try { Thread.sleep(1000); Message msg = new Message(); msg.what = 1; handler.sendMessage(msg); System.out.println("send..."); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); System.out.println("thread error..."); } } } } }
方法二:Handler類自帶的postDelyed
package com.xunfang.handerDemo; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.widget.TextView; /** * handler定時器使用postDelyed實現 * * @author Smalt * */ public class HanderDemoActivity extends Activity { TextView tvShow; private int i = 0; private int TIME = 1000; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); tvShow = (TextView) findViewById(R.id.tv_show); handler.postDelayed(runnable, TIME); //每隔1s執行 } Handler handler = new Handler(); Runnable runnable = new Runnable() { @Override public void run() { // handler自帶方法實現定時器 try { handler.postDelayed(this, TIME); tvShow.setText(Integer.toString(i++)); System.out.println("do..."); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); System.out.println("exception..."); } } }; }
方法三:Handler+Timer+TimerTask
package com.xunfang.handerDemo; import java.util.Timer; import java.util.TimerTask; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.widget.TextView; /** * 定時器實現:Handler+Timer+TimerTask * * @author Smalt * */ public class HanderDemoActivity extends Activity { TextView tvShow; private int i = 0; private int TIME = 1000; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); tvShow = (TextView) findViewById(R.id.tv_show); timer.schedule(task, 1000, 1000); // 1s後執行task,經過1s再次執行 } Handler handler = new Handler() { public void handleMessage(Message msg) { if (msg.what == 1) { tvShow.setText(Integer.toString(i++)); } super.handleMessage(msg); }; }; Timer timer = new Timer(); TimerTask task = new TimerTask() { @Override public void run() { // 需要做的事:發送消息 Message message = new Message(); message.what = 1; handler.sendMessage(message); } }; }
以上就是對Android 定時器的資料整理後續繼續補充相關知識,謝謝大家對本站的支持!
東東手游助手是款官方推出的安卓模擬器,在東東手游助手運行時出現閃退這種異常現象是因為什麼呢?怎麼解決東東手游助手閃退問題呢?先分析東東手游助手閃退原因:1.
android開發最常用例子整理----(1)自定義按鈕實現 一、Activity MainActivity.java源碼: public class MainA
概要設計模式是一門藝術,如果真正了解這門藝術,你會發現,世界都將變得更加優美。定義策略模式定義了一系列的算法,並將每一個算法封裝起來,他們之間還可以相互替換,策略模式獨立
想將Button和ListView分別放在屏幕的一左一右。單純使用android:gravity和android:layout_gravity不成功。於是涉及到Relat