我正在测试不同API的AVD。在4.0.3上工作正常,但是当我用2.3.3测试它时,应用程序崩溃并且AVD每次都重新启动。
这是我的服务类:
public class SendIntentService extends Service{
@Override
public int onStartCommand(Intent intent, int flags, int startId){
MyLocationListener myLocationListener = new MyLocationListener(this);
Location location = myLocationListener.getLocation();
double latitude = location.getLatitude();
double longtitude = location.getLongtitude();
Log.e("TEST", "lat: " + latitude + " | long: " + longtitude);
return super.onStartCommand(intent, flags, startId);
}
}
这就是我获取用户位置的方式:
public class MyLocationListener implements LocationListener {
private Context context;
private Location location;
private LocationManager locationManager;
private double longtitude = 0.0;
private double latitude = 0.0;
public MyLocationListener(Context context){
this.context = context;
}
public Location getLocation(){
locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
boolean isGpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
boolean isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGpsEnabled && !isNetworkEnabled) return null;
if (isNetworkEnabled){
locationManager.requestLocationUpdates( LocationManager.NETWORK_PROVIDER,
Constants.MIN_INTERVAL,
Constants.LOC_DISTANCE,
this);
if (locationManager != null){
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null){
latitude = location.getLatitude();
longtitude = location.getLongitude();
}
}
}
if (isGpsEnabled){
if (location == null){
locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER,
Constants.MIN_INTERVAL,
Constants.LOC_DISTANCE,
this);
if (locationManager != null){
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null){
latitude = location.getLatitude();
longtitude = location.getLongitude();
}
}
}
}
return location;
}
public double getLatitude(){
return latitude;
}
public double getLongitute(){
return longtitude;
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
权限在AndroidManifest中设置。正如我所说,在4.0.3上工作得很好,但在2.3.3上应用程序崩溃了。我不知道为什么。