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.dbjr.com.cn/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 Canvas方法總結(jié)最全面詳解API(小結(jié))
本篇文章主要介紹了Android Canvas方法總結(jié)最全面詳解API(小結(jié)),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-11-11Android中 自定義數(shù)據(jù)綁定適配器BaseAdapter的方法
本篇文章小編為大家介紹,Android中 自定義數(shù)據(jù)綁定適配器BaseAdapter的方法。需要的朋友參考下2013-04-04Android 判斷某個(gè)服務(wù)(service)是否運(yùn)行
這篇文章主要介紹了 Android 判斷某個(gè)服務(wù)(service)是否運(yùn)行的相關(guān)資料,需要的朋友可以參考下2017-06-06Android?App實(shí)現(xiàn)閃屏頁(yè)廣告圖的全屏顯示實(shí)例
這篇文章主要為大家介紹了Android?App實(shí)現(xiàn)閃屏頁(yè)廣告圖的全屏顯示實(shí)例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08Android手機(jī)開發(fā) 控件 TextView文字居中
本文主要介紹Android手機(jī)開發(fā)TextView居中的方法,希望能幫到大家。2016-05-05詳解Android平臺(tái)JSON預(yù)覽(JSON-handle)
這篇文章主要介紹了Android平臺(tái)JSON預(yù)覽(JSON-handle),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-09-09Android實(shí)現(xiàn)從相冊(cè)選擇照片功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)從相冊(cè)選擇照片功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03