4

I have a service which has a BeaconNotificationsManager, I want to access this BeaconNotificationsManager in my Activity. Currently my BeaconNotificationsManager is static:

public class MyService extends Service { 
    public static BeaconNotificationsManager bnm;
}

And I am accessing this in my Activity like this:

if(MyService.bnm != null){
     // do stuff
}

Although Android is telling me this is bad. What is the correct way to do this?

TT--
  • 2,956
  • 1
  • 27
  • 46
Nickmccomb
  • 1,467
  • 3
  • 19
  • 34

1 Answers1

3

About Static issue: let just say you are referencing your service bnm from another class and your service has been destroyed by the OS but the static object(bnm) is still in use by some activity so this will hold on the service context from garbage collection unless you set your bnm reference inside your activity to null and this will leak all the application's resources

Solution :

The optimal option is use BindService in this way you will get the more control over your service through the object of service , in service use IBinder

class MyService..{
   public BeaconNotificationsManager bnm;
   public class LocalBinder extends Binder {
        LocalService getService() {
            // Return this instance of LocalService so clients can call public methods
            return LocalService.this;
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }

   // inside service class
    public boolean getStatus(){
     return bnm==null;
    }
}

So when you bind a service , you will get the binder object which can further give you the service object and use your function to check nullity

1.) Create a ServiceConnection object

  private ServiceConnection mConnection = new ServiceConnection() {

        @Override
        public void onServiceConnected(ComponentName className,
                IBinder service) {
            // We've bound to LocalService, cast the IBinder and get LocalService instance
            LocalBinder binder = (LocalBinder) service;
            mService = binder.getService();
            mBound = true;
            bnmNull= mService.getStatus(); // bnm status
        }

2.) Bind a Service using ServiceConnection object created in first step

    Intent intent = new Intent(this, MyService.class);
    bindService(intent, mConnection, Context.BIND_AUTO_CREATE);

,so then simply have a function in your class 'getStatus' and call it with the object retrieved through the binder check out the link for code example

Pavneet_Singh
  • 36,884
  • 5
  • 53
  • 68