2011-05-02 5 views
0

지도 데이터보기를 통해 활동이 시작된 이후의 시간을 표시하고 싶습니다.Android 타이머 중지 및 시작

여기는 내가 사용하고있는 타이머 코드입니다. 5 분 후에 타이머가 멈추길 바래요. 이 코드를 작동 시키려면이 코드에 무엇을 추가해야합니까?

<?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> 

가 현재 나는 더 이상 내지도보기를 참조하지, 난 단지 내 텍스트 뷰가 시간을 표시 참조 :

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입니다.

답변

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

?

+0

어떻게하면 Google지도에서 타이머를 볼 수 있습니까? – anji

+0

@koko Google지도는 수정할 수 없습니다. 귀하의 활동은 'com.google.android.maps.MapView'를 사용하여 사용자에게지도를 보여줄 수 있습니다. – Jerome

+0

이 코드를 이미지도를 보여주는 프로젝트에 추가합니다. 그러나이 코드를 추가하면지도가 사라지고지도의 오버레이 항목조차도 사라집니다. – anji

0

이 코드는 활동이 실행 된 시간을 계산하고이 수량을 표시하는 것처럼 보입니다.

활동을 중지하려면 원하는 조건이 충족 될 때 update30View 메소드에서 mHandler.postDelayed(mUpdateTime, 1000);을 호출하는 것을 중지해야합니다. ColdForged 제안은 당신의 텍스트 뷰와 동시에 표시하도록지도보기 모두 다음과 같은 XML 사용한다 얻으려면 당신의 기준

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

을 충족

<?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); 
} 
관련 문제