我想使用 gps 提供程序获取设备的当前位置,但这实际上是在等待 gps 位置发生变化。
所以我有一个简单的应用程序来监听位置变化。这可以NETWORK_PROVIDER
立即使用,但是GPS_PROVIDER
我等了 15 分钟并且没有发生任何动作。
我正在使用 API 级别 8。
public class MainActivity extends Activity {
TextView tv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) this.findViewById(R.id.textView1);
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
LocationListener ll = new mylocationlistener();
//lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, ll); // this works fine
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
}
private class mylocationlistener implements LocationListener {
public void onLocationChanged(Location location) {
Date today = new Date();
Timestamp currentTimeStamp = new Timestamp(today.getTime());
if (location != null) {
Log.d("LOCATION CHANGED", location.getLatitude() + "");
Log.d("LOCATION CHANGED", location.getLongitude() + "");
String str = "\n CurrentLocation: " + "\n Latitude: "
+ location.getLatitude() + "\n Longitude: "
+ location.getLongitude() + "\n Accuracy: "
+ location.getAccuracy() + "\n CurrentTimeStamp "
+ currentTimeStamp + "\n Altitude: "
+ location.getAltitude() + "\n Bearing"
+ location.getBearing() + "\n Speed"
+ location.getSpeed() + "\n ";
Toast.makeText(MainActivity.this, str, Toast.LENGTH_LONG)
.show();
tv.append(str);
}
}
public void onProviderDisabled(String provider) {
Toast.makeText(MainActivity.this, "Error onProviderDisabled",
Toast.LENGTH_LONG).show();
}
public void onProviderEnabled(String provider) {
Toast.makeText(MainActivity.this, "onProviderEnabled",
Toast.LENGTH_LONG).show();
}
public void onStatusChanged(String provider, int status, Bundle extras) {
Toast.makeText(MainActivity.this, "onStatusChanged",
Toast.LENGTH_LONG).show();
}
}
}
我确实将许可放在清单中,所以这不是问题。