Android Service access to static member of another class

1.3k views Asked by At

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.

3

There are 3 answers

8
megaturbo On BEST ANSWER

You use Device.getLocationType() are you sure this method is also static ? So either make it static or make locationType public and access it directly.

Whether you'd want to is another thing entirely.

Also, try to replace :

private static LocationType locationType; 

with :

private static LocationType locationType = new LocationType();
3
Raymond On

You field is private

private static LocationType locationType;

Make it public and it should be accessible

public static LocationType locationType;

If that isn't desired you should at least make a static getter which is either public or package private. If it's package private, make sure both classes are within the same package.

0
Banana On

My object "locationType" is always defined to null... In my MainActivity, i call the method Device.init(...). This method create the object LocationType in the static member.

public class Device {
private static LocationType locationType;

 public static void init(...){
    Device.locationType = new LocationType(...);

    if(Device.locationType != null){
        Intent i = new Intent(activity, LocationService.class);
        activity.startService(i);
    }
}

My service is launched, but in the service method "onLocationChanged", the Device.locationType is null.