在我正在制作的 gps 应用程序上更改经度和纬度似乎有一些问题。我是 Android 新手,不能 100% 确定我做对了。我还想指出,我不想仅在位置更改时才使用侦听器。我特别希望它每 5 秒更新一次,因为我在线程中有它。我似乎也没有从我在下面使用的东西中检索任何卫星数据。我只是一直在我的日志文件中写入一个空列表。我错过了许可还是代码错了?下面提供了所有需要的信息。谢谢。
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize various GPS things
location = new Location("now");
lm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
gps = lm.getGpsStatus(null);
// Create Entry List for later use
entries = new ArrayList<Entry>();
rows = new ArrayList<TableRow>();
// Turn off Stop by default
Button button = (Button)findViewById(R.id.stopButton);
button.setEnabled(false);
// GPS enabled?
final LocationManager manager = (LocationManager) getSystemService( Context.LOCATION_SERVICE );
if ( !manager.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) {
buildAlertMessageNoGps();
}
criteria = new Criteria();
criteria.setBearingAccuracy(Criteria.ACCURACY_HIGH);
criteria.setAltitudeRequired(true);
criteria.setHorizontalAccuracy(Criteria.ACCURACY_HIGH);
criteria.setAccuracy(Criteria.ACCURACY_FINE);
backgroundThread.start();
}
Thread thread = new Thread()
{
@Override
public void run() {
while (keepGoing) {
// timestamp
long ts = System.currentTimeMillis() / 1000L;
// set location
location = lm.getLastKnownLocation(lm.getBestProvider(criteria, true));
// location stuff
float ac = location.getAccuracy();
double al = location.getAltitude();
double lat = location.getLatitude();
double lon = location.getLongitude();
DataEntry d = new DataEntry(ac, al, lat, lon);
// satellite stuff
List<GPSSatelliteEntry> GPSentries = new ArrayList<GPSSatelliteEntry>();
Iterable<GpsSatellite> sats = gps.getSatellites();
Iterator<GpsSatellite> satI = sats.iterator();
while (satI.hasNext()) {
GpsSatellite item = (GpsSatellite) satI.next();
float az = item.getAzimuth();
float el = item.getElevation();
int p = item.getPrn();
float s = item.getSnr();
GPSSatelliteEntry temp = new GPSSatelliteEntry(az, el, p, s);
GPSentries.add(temp);
}
Entry en = new Entry(ts, d, GPSentries);
entries.add(en);
try {
Thread.sleep(5000); // 5000 ms
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
writeData();
}
};