2011-05-02 95 views
0

我想顯示自從活動在地圖數據視圖上開始以來的時間。Android定時器停止並開始

這是我正在使用的計時器代碼。我希望計時器在5分鐘後停止。我必須添加到此代碼才能使其工作?

package timer.tr; 

import android.app.Activity; 
import android.os.Bundle; 
import android.os.Handle; 
import android.os.Handler; 
import android.widget.TextView; 

public class timer extends Activity { 
    private TextView timeView; 
    private int hour = 0; 
    private int min = 0; 
    private int sec = 0; 
    String mTimeFormat = "%02d:%02d:%02d"; 
    final private Handler mHandler = new Handler(); 
    Runnable mUpdateTime = new Runnable() { 
     public void run() { updateTimeView(); } 
    }; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     timeView = new TextView(this); 
     timeView.setText(String.format(mTimeFormat, hour, min, sec)); 
     setContentView(timeView); 
     mHandler.postDelayed(mUpdateTime, 1000); 
    } 

    public void updateTimeView() { 
     sec += 1; 
     if(sec >= 60) { 
      sec = 0; 
      min += 1; 
      if (min >= 60) { 
       min = 0; 
       hour += 1; 
      } 
     } 
     timeView.setText(String.format(mTimeFormat, hour, min, sec)); 
     mHandler.postDelayed(mUpdateTime, 1000); 
    } 
} 

這裏是我的佈局XML:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
> 
<com.google.android.maps.MapView 
    android:id="@+id/mapView" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:enabled="true" 
    android:clickable="true" 
    android:apiKey="0cWhB29uXURimKWeF3lASx-MHSdekdEUZ1oZ-MQ" 
    /> 
    <RelativeLayout android:layout_height="wrap_content" android:id="@+id/relativeLayout1" android:layout_width="fill_parent"></RelativeLayout> 
    </LinearLayout> 

目前,我再也看不到我的地圖視圖,我只看到我的TextView顯示的時間。

回答

0
... 
timeView.setText(String.format(mTimeFormat, hour, min, sec)); 
if(min < 5) 
    mHandler.postDelayed(mUpdateTime, 1000); 

+0

如何查看Google地圖中的計時器? – anji 2011-05-02 22:34:59

+0

@koko您無法修改Google地圖。儘管您的活動可以使用「com.google.android.maps.MapView」向用戶顯示地圖。 – Jerome 2011-05-02 22:37:38

+0

我將此代碼添加到已顯示地圖的我的項目中,但是當我添加此代碼時,地圖消失並且只有計數纔會出現,即使地圖中的疊加項消失 – anji 2011-05-02 22:42:17

0

此代碼看起來像它計算您的活動已運行的時間並顯示此數量。

如果您希望活動停止,您應該在您滿足所需條件時停止在updateTimeView方法中調用mHandler.postDelayed(mUpdateTime, 1000);。 ColdForged建議符合您的標準

if(min < 5) 
    mHandler.postDelayed(mUpdateTime, 1000); 

要同時獲得您的TextView和你的MapView顯示在同一時間,你應該使用下面的XML:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
> 
<com.google.android.maps.MapView 
    android:id="@+id/mapView" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:enabled="true" 
    android:clickable="true" 
    android:apiKey="0cWhB29uXURimKWeF3lASx-MHSdekdEUZ1oZ-MQ" 
    /> 
<TextView android:id="@+id/timeText" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="" /> 
</RelativeLayout> 

你應該修改onCreate方法的以下內容:

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    timeView = findViewById(R.id.timeText); 
    timeView.setText(String.format(mTimeFormat, hour, min, sec)); 
    setContentView(R.layout.your_xml_filename_here); 
    mHandler.postDelayed(mUpdateTime, 1000); 
}