さまざまな 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 ではアプリがクラッシュします。どうしてか分かりません。