Do not place Android context classes in static fields; this is a memory leak

前端 未结 1 1439
长发绾君心
长发绾君心 2021-01-13 18:57

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

相关标签:
1条回答
  • 2021-01-13 19:24

    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

    0 讨论(0)
提交回复
热议问题