2011-07-16 2 views
4


나는 안드로이드에서 위성 정보를 얻으려고하고 어떤 결과를주지 않는이 다음 코드를 썼다. 왜 그런가?
android에서 위성에 대한 정보를 얻는 방법?

public void onCreate(Bundle savedInstanceState){ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.gps); 

    final LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 

    GpsStatus gpsStatus = locationManager.getGpsStatus(null); 
    if(gpsStatus != null) 
    { 
     Iterable<GpsSatellite>satellites = gpsStatus.getSatellites(); 
     Iterator<GpsSatellite>sat = satellites.iterator(); 
     int i=0; 
     while (sat.hasNext()) { 
      GpsSatellite satellite = sat.next(); 


      strGpsStats+= (i++) + ": " + satellite.getPrn() + "," + satellite.usedInFix() + "," + satellite.getSnr() + "," + satellite.getAzimuth() + "," + satellite.getElevation()+ "\n\n"; 
     } 
    } 
    TextView tv = (TextView)(findViewById(R.id.Gpsinfo)); 
    tv.setText(strGpsStats); 
} 

감사
Nohsib LocationManager.getGpsStatus(...)의 문서에 따르면

답변

10

...

이 데이터 만 원자 적으로 복사되는 것을 보장하기 위해 onGpsStatusChanged(int) 콜백에서 호출해야합니다.

GpsStatus.Listener을 활동에 구현하고 onGpsStatusChanged(int)을 재정의하십시오. 예 ...

public class MyActivity extends Activity implements GpsStatus.Listener { 

    LocationManager locationManager = null; 
    TextView tv = null; 

    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.gps); 

     tv = (TextView)(findViewById(R.id.Gpsinfo)); 
     locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
     locationManager.addGpsStatusListener(this); 
    } 

    @Override 
    public void onGpsStatusChanged(int) { 
     GpsStatus gpsStatus = locationManager.getGpsStatus(null); 
     if(gpsStatus != null) { 
      Iterable<GpsSatellite>satellites = gpsStatus.getSatellites(); 
      Iterator<GpsSatellite>sat = satellites.iterator(); 
      int i=0; 
      while (sat.hasNext()) { 
       GpsSatellite satellite = sat.next(); 
       strGpsStats+= (i++) + ": " + satellite.getPrn() + "," + satellite.usedInFix() + "," + satellite.getSnr() + "," + satellite.getAzimuth() + "," + satellite.getElevation()+ "\n\n"; 
      } 
      tv.setText(strGpsStats); 
     } 
    } 
} 
+0

"strGpsStats"란 무엇입니까? 문자열 ?? – Rich

+0

@Rich : 나는 'str'접두어와'TextView'의 텍스트를 설정하는 데 사용되는 사실을 기반으로한다고 가정합니다. 나는 단순히 strGpsStats가 포함 된 그들의 질문에서 OP의 코드를 수정하고 있었다. – Squonk

0

이 시도 :

LocationManager locmgr = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); 
    //locmgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10L, 5.0f, this); 
    Location location = locmgr.getLastKnownLocation(LocationManager.GPS_PROVIDER); 

    if (location == null) { 
     location = locmgr.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 

    } 
    if (location == null) { 
     location = locmgr.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER); 

    } 

가 알려줘 어떤 문제가있는 경우.

관련 문제