Android Timer使用的實(shí)例代碼
1:服務(wù)端使用PHP
<?php
echo date('Y-m-d H:i:s');
?>
2:activity_main.xml
<RelativeLayout 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">
<Button
android:id="@+id/btn_click"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button Start"/>
<TextView
android:id="@+id/tv_show"
android:layout_below="@id/btn_click"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
android:textSize="28sp"/>
<Button
android:id="@+id/btn_stop"
android:layout_below="@id/tv_show"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button Stop"/>
</RelativeLayout>
3:MainActivity.java
HttpHelper.getStringFromNet2(param)此方法見:http://www.dhdzp.com/article/42126.htm
public class MainActivity extends Activity {
private Button btnClick=null;
private Button btnStop=null;
private TextView tvShow=null;
private String info="";
private Timer timer=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnClick=(Button)findViewById(R.id.btn_click);
btnStop=(Button)findViewById(R.id.btn_stop);
tvShow=(TextView)findViewById(R.id.tv_show);
timer=new Timer();
btnClick.setOnClickListener(new OnClickListener(){
public void onClick(View view){
timer.scheduleAtFixedRate(new MyTask(), 100, 2000);
}
});
btnStop.setOnClickListener(new OnClickListener(){
public void onClick(View view){
timer.cancel();
}
});
}
Handler myHandler=new Handler(){
public void handleMessage(Message msg){
if(info!=""){
tvShow.setText(info);
}
}
};
private class MyTask extends TimerTask{
public void run(){
String param="http://192.168.0.116/android/time.php";
info=HttpHelper.getStringFromNet2(param);
myHandler.obtainMessage(100).sendToTarget();
}
}
}
4:運(yùn)行結(jié)果:
- 詳解Android中提示對(duì)話框(ProgressDialog和DatePickerDialog和TimePickerDialog&PopupWindow)
- android中DatePicker和TimePicker的使用方法詳解
- Android基于CountDownTimer實(shí)現(xiàn)倒計(jì)時(shí)功能
- Android中CountDownTimer倒計(jì)時(shí)器用法實(shí)例
- Android編程獲取網(wǎng)絡(luò)時(shí)間實(shí)例分析
- Android中日期與時(shí)間設(shè)置控件用法實(shí)例
- android獲取時(shí)間差的方法
- Android調(diào)用系統(tǒng)時(shí)間格式顯示時(shí)間信息
- Android 桌面Widget開發(fā)要點(diǎn)解析(時(shí)間日期Widget)
- 解析android中系統(tǒng)日期時(shí)間的獲取
- Android獲取通話時(shí)間實(shí)例分析
- android 默認(rèn)時(shí)間格式修改方法
- Android開發(fā)之TimePicker控件用法實(shí)例詳解
相關(guān)文章
Android自定義控件開發(fā)實(shí)戰(zhàn)之實(shí)現(xiàn)ListView下拉刷新實(shí)例代碼
這篇文章主要介紹了Android自定義控件開發(fā)實(shí)戰(zhàn)之實(shí)現(xiàn)ListView下拉刷新實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2016-04-04
Android中WebView與Js交互的實(shí)現(xiàn)方法
本文給大家介紹android中webview與js交互的實(shí)現(xiàn)方法,本文介紹的非常詳細(xì),具有參考借鑒價(jià)值,感興趣的朋友一起學(xué)習(xí)2016-05-05
Android入門之IntentService的使用教程詳解
IntentService的生命周期中有一個(gè)非常好的方法-onHandleIntent方法,它是一個(gè)abstract方法,開發(fā)者在實(shí)現(xiàn)IntentService時(shí)可以覆蓋它來處理“長(zhǎng)事務(wù)”。本文就來聊聊IntentService的使用,需要的可以參考一下2022-12-12
Android圖片上傳實(shí)現(xiàn)預(yù)覽效果
這篇文章主要介紹了Android圖片上傳實(shí)現(xiàn)預(yù)覽效果的相關(guān)資料,需要的朋友可以參考下2016-01-01
Android ShimmerLayout實(shí)現(xiàn)微光效果解析
這篇文章主要為大家詳細(xì)介紹了Android ShimmerLayout實(shí)現(xiàn)微光效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03

