2014-01-18 3 views
1

나는 안드로이드 초보자이며 도움이 필요합니다! 매우 간단한 실행 앱을 만들고 속도에 대한 정보를 수집하고 싶습니다. 이를 위해 getspeed() 메서드를 사용하기 위해 아주 간단한 GPS 클래스를 생성하려고합니다.Android : GPS 좌표는 에뮬레이터에서 작동하지만 휴대 전화에서는 작동하지 않습니다.

내 코드가 올바른지 확인하기 위해 약간의 테스트 앱을 만들었습니다. 이 응용 프로그램에서는 getLatitude() 및 getLongitude()도 사용하고 있지만이 코드는 최종 코드에 유용하지 않습니다. 속도는 실제로 필요합니다.

지금까지 필자는 에뮬레이터에서 클래스를 성공적으로 테스트했지만 전화에서는 테스트하지 못했습니다. DDMS 좌표 (getspeed()가 0 인 경우)를 사용할 때 화면에 채워지는 좌표를 볼 수 있지만 에뮬레이터에서이 점을 테스트 할 수는 없습니다. 내 휴대 전화에서 코드를 시도 할 때 전혀 응답이 없습니다 - GPS가 "예열"(Google지도로 이동하면 GPS가 작동하는 것을 볼 수 있음)를 기다리는데도 불구하고.

모든 것이 나를 미치게합니다. onLocationChanged() 메소드로 어떤 일이 일어나고 있는지 확신 할 수 없으므로 저에게 줄 수있는 도움에 감사드립니다. 감사합니다.

아래의 코드는 당과 같습니다 그 방법이 완료되면 당신이 위치 업데이트를하지 않도록

import android.app.Activity; 
    import android.content.Context; 
    import android.location.Location; 
    import android.location.LocationListener; 
    import android.location.LocationManager; 
    import android.os.Bundle; 
    import android.widget.TextView; 
    import android.widget.Toast; 

    public class MainActivity extends Activity { 
    TextView speedText1, speedText2, speedText3; 
double speed = 0; 
double latitude = 0; 
double longitude = 0; 
String speed1; 
String latitude1; 
String longitude1; 
boolean gps_enabled; 

Context context; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); 


    LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); 
    gps_enabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); 


    LocationListener locationListener = new LocationListener() { 
     public void onLocationChanged(Location location) { 

      Toast.makeText(getBaseContext(), "location not null" , Toast.LENGTH_SHORT).show(); 


      speedText1 = (TextView) findViewById(R.id.textView1); 
      speedText2 = (TextView) findViewById(R.id.textView2); 
      speedText3 = (TextView) findViewById(R.id.textView3); 

      speed = location.getSpeed(); 
      speed1 = Double.toString(speed); 
      speedText1.setText(speed1); 

      latitude = location.getLatitude(); 
      latitude1 = Double.toString(latitude); 
      speedText2.setText(latitude1); 

      longitude = location.getLongitude(); 
      longitude1 = Double.toString(longitude); 
      speedText3.setText(longitude1); 

      Toast.makeText(getBaseContext(), latitude1 + longitude1 + speed , Toast.LENGTH_SHORT).show(); 



     }  

     public void onStatusChanged(String provider, int status, Bundle extras) { 

     } 

     public void onProviderEnabled(String provider) { 

     } 

     public void onProviderDisabled(String provider) { 

     } }; 
     locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener); 
    } 
} 
+0

가능한 중복 http://stackoverflow.com/questions/4811920/why-getspeed-always ([왜 getSpeed ​​()는 항상 안드로이드에 0을 리턴] : 당신은 이런 식으로 뭔가를 시도해야한다 -return-0-on-android) – FWeigl

+0

아니요, 에뮬레이터에서 작동하지 않는 getSpeed ​​()에 대해 알고 있습니다. 나는이 코드가 전화기에서 작동하지 않는 이유를 아는 사람이 있는지 궁금했다. – user2943342

답변

2

당신은에서 onCreate() 메소드 내부의 LocationListener를 정의하고 있습니다.

import android.app.Activity; 
import android.content.Context; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.Bundle; 
import android.widget.TextView; 
import android.widget.Toast; 

public class MainActivity extends Activity implements LocationListener { 

TextView speedText1, speedText2, speedText3; 
double speed = 0; 
double latitude = 0; 
double longitude = 0; 
String speed1; 
String latitude1; 
String longitude1; 
boolean gps_enabled; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 


    LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); 
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1, 0.0f, this); 
    gps_enabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); 


    Toast.makeText(this, "location not null" , Toast.LENGTH_SHORT).show(); 


    speedText1 = (TextView) findViewById(R.id.textView1); 
    speedText2 = (TextView) findViewById(R.id.textView2); 
    speedText3 = (TextView) findViewById(R.id.textView3); 

    } 

    @Override 
    public void onLocationChanged(Location location) { 

     speed = location.getSpeed(); 
     speed1 = Double.toString(speed); 
     speedText1.setText(speed1); 

     latitude = location.getLatitude(); 
     latitude1 = Double.toString(latitude); 
     speedText2.setText(latitude1); 

     longitude = location.getLongitude(); 
     longitude1 = Double.toString(longitude); 
     speedText3.setText(longitude1); 

     Toast.makeText(this, latitude1 + longitude1 + speed, Toast.LENGTH_SHORT).show();    

    } 

} 

}의

+0

당신은 스타입니다! 코드가 의도 한대로 작동합니다. 고마워요 파블로! – user2943342

관련 문제