我在Android上与卫星争吵,问题是我无法获得卫星阵列。事实上,我可以获得数组,但它没有任何元素,这就是为什么sats.iterator()。hasNext()返回false的原因。谁能告诉我为什么我的代码不起作用。
这是我的Java代码
package com.example.location;
import java.util.Iterator;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.content.Context;
import android.location.GpsSatellite;
import android.location.GpsStatus;
import android.location.LocationManager;
import android.os.Bundle;
public class MainActivity extends ActionBarActivity implements
GpsStatus.Listener {
GpsStatus gpsStatus;
GpsSatellite gpsSatellite;
Iterable<GpsSatellite> sats;
LocationManager locationManager = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
locationManager = (LocationManager) this
.getSystemService(Context.LOCATION_SERVICE);
locationManager.addGpsStatusListener(this);
}
public void onclick(View v) {
TextView tvNum = (TextView) findViewById(R.id.tvNum);
onGpsStatusChanged(GpsStatus.GPS_EVENT_SATELLITE_STATUS);
if (gpsStatus != null) {
int num = gpsStatus.getMaxSatellites();
Log.d("Number of Satellines: ", String.valueOf(num));
tvNum.setText(num + "");
if (sats != null) {
Log.d("Get Satellites: ", String.valueOf("Ok"));
Log.d("Sats", String.valueOf(sats.toString()));
Iterator<GpsSatellite> sat = sats.iterator();
if (sat != null) {
Log.d("get done", String.valueOf(sat.toString()));
Log.d("hasNext_State",
String.valueOf(sats.iterator().hasNext()));
int i = 1;
while (sat.hasNext()) {
Log.d("got it", String.valueOf("OK"));
GpsSatellite sat1 = sat.next();
Log.d("Satellite " + i + ":",
String.valueOf(sat1.getSnr()));
i++;
}
} else
Log.d("Can't get f1", String.valueOf("..."));
} else
Log.d("Can't get f2", String.valueOf("..."));
} else
Log.d("Can't get f3", String.valueOf("..."));
}
@Override
public void onGpsStatusChanged(int event) {
// TODO Auto-generated method stub
if (event == GpsStatus.GPS_EVENT_SATELLITE_STATUS) {
gpsStatus = locationManager.getGpsStatus(null);
sats = gpsStatus.getSatellites();
} else
gpsStatus = null;
}
}