我为安卓开发了一个基于 GPS 的应用程序。尽管该应用程序适用于所有其他设备,但位置侦听器在 Galaxy Note 2 设备中失败。
启用 GPS:
if (locationManager == null) {
locationManager = (LocationManager) this
.getSystemService(Context.LOCATION_SERVICE);
}
if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)
&& !locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
_objSLM.isGPSEnable = false;
}
startService(new Intent(this, SLGPSService.class));
附加监听器:
if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 0, 100, locationListener);
}
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, 0, 100, locationListener);
}
if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)
&& !locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
try {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 0, 100,
locationListener);
} catch (Exception e) {
}
try {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, 0, 100, locationListener);
} catch (Exception e) {
}
}
位置监听器:
LocationListener locationListener = new LocationListener() {
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.v(TAG, "**********=======Status=" + status);
}
public void onProviderEnabled(String provider) {
// _objSLM.isGPSEnable = true;
if (locationManager.isProviderEnabled(provider)) {
if (locationManager.getLastKnownLocation(provider) != null) {
_objSLM.curLocation = locationManager
.getLastKnownLocation(provider);
_objSLM.isGPSEnable = true;
if (_objSLM.prevLocation == null) {
_objSLM.prevLocation = _objSLM.curLocation;
}
}
}
if (locationManager.isProviderEnabled(provider)) {
if (locationManager.getLastKnownLocation(provider) != null) {
_objSLM.curLocation = locationManager.getLastKnownLocation(provider);
_objSLM.isGPSEnable = true;
if (_objSLM.prevLocation == null) {
_objSLM.prevLocation = _objSLM.curLocation;
}
}
}
}
public void onProviderDisabled(String provider) {
_objSLM.isGPSEnable = false;
}
public void onLocationChanged(Location location) {
Log.v(TAG, location.getLatitude() + "==" + location.getLongitude()+=================onLocationChanged()");
if(location.getProvider().equals(LocationManager.GPS_PROVIDER)){
_objSLM.curLocation = location;
_objSLM.isGPSEnable = true;
}
if (_objSLM.prevLocation == null) {
_objSLM.prevLocation = _objSLM.curLocation;
}
}
};
权限:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />