我想使用gps提供程序獲取設備的當前位置,但實際上等待gps位置發生變化。如何獲得當前的GPS位置而無需等待Android上的位置更改?
所以我有一個簡單的應用程序偵聽的位置變化。這適用於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();
}
}
}
我沒有把清單中的權限,所以這不是問題。
你確定你已經啓用了您的GPS? – gsingh2011
是的,我很確定... –
這取決於您的設備以及GPS信號強度,因爲較新的手機具有高敏感的GPS系統,但舊手機需要開闊的天空來檢測信號。 –