I have a problem with my android service. In the "onLocationChanged", i want to access a static member of my class "Device".
I start my service with :
Intent i = new Intent(this, LocationService.class);
startService(i);
This is my class LocationListener with the service:
private class LocationListener implements android.location.LocationListener{
Location mLastLocation;
LocationListener(String provider)
{
Log.e(TAG, "LocationListener " + provider);
mLastLocation = new Location(provider);
}
@Override
public void onLocationChanged(Location location)
{
Device.getLocationType().setLocationData(location);
}
@Override
public void onProviderDisabled(String provider)
{
//...
}
@Override
public void onProviderEnabled(String provider)
{
//...
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras)
{
//...
}
}
LocationListener[] mLocationListeners = new LocationListener[] {
new LocationListener(LocationManager.GPS_PROVIDER),
new LocationListener(LocationManager.NETWORK_PROVIDER)
};
@Override
public IBinder onBind(Intent arg0)
{
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
super.onStartCommand(intent, flags, startId);
Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show();
Log.d(TAG, "onStartCommand");
return START_STICKY;
}
@Override
public void onCreate()
{
super.onCreate();
Log.d(TAG, "onCreate");
initializeLocationManager();
try {
mLocationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, MIN_TIME, MIN_DISTANCE,
mLocationListeners[1]);
Log.d(TAG, "try");
} catch (java.lang.SecurityException ex) {
Log.d(TAG, "fail to request location update, ignore", ex);
} catch (IllegalArgumentException ex) {
Log.d(TAG, "network provider does not exist, " + ex.getMessage());
}
try {
mLocationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, MIN_TIME, MIN_DISTANCE,
mLocationListeners[0]);
Log.d(TAG, "try2");
} catch (java.lang.SecurityException ex) {
Log.d(TAG, "fail to request location update, ignore", ex);
} catch (IllegalArgumentException ex) {
Log.d(TAG, "gps provider does not exist " + ex.getMessage());
}
}
@Override
public void onDestroy()
{
Log.e(TAG, "onDestroy");
super.onDestroy();
if (mLocationManager != null) {
for (int i = 0; i < mLocationListeners.length; i++) {
try {
mLocationManager.removeUpdates(mLocationListeners[i]);
} catch (Exception ex) {
Log.i(TAG, "fail to remove location listners, ignore", ex);
}
}
}
}
private void initializeLocationManager() {
Log.e(TAG, "initializeLocationManager");
if (mLocationManager == null) {
mLocationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
}
}
My class Device
public class Device {
private static LocationType locationType;
//...
}
And the class LocationType
public class LocationType {
private Location location;
public LocationType(){
//...
}
Location getLocationData() {
return location;
}
void setLocationData(Location location) {
this.location = location;
}
}
My application failed because, i don't have the right to access static member from service? How can i access this static member?
Thanks a lot for your answer.
You use
Device.getLocationType()
are you sure this method is also static ? So either make it static or makelocationType
public and access it directly.Whether you'd want to is another thing entirely.
Also, try to replace :
with :