我为Android开发了一款基于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;
}
}
};
Permissions:
<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" />
答案 0 :(得分:0)
可能这不是您的问题,这是设备特定的问题。
您必须检查 Android版并更新 manifest.xml 文件。