5

I have a service created like this :

<service
    android:name="com.myFirebaseMessagingService">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT"/>
    </intent-filter>
</service>

Then I implement the onBind like this:

  private final IBinder mBinder = new LocalBinder();
  private myListener mListener;


  public class LocalBinder extends Binder {
    LocalService getService() {
      return LocalService.this;
    }
  }

  public void setListener(myListener listener) {
    mListener = listener;
  }    

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

  @Override
  public void onMessageReceived(RemoteMessage remoteMessage) {
    if (mListener != null) mListener.onMessageReceived(remoteMessage);  
  }

It is quite simple: the Activity binds to the Service and sets up a listener. When the Service receives a message it simply fires the listener

Now the big question: what happen if the activity crash suddenly? In that case mListener will point to something non-existent, no?

How, before calling mListener.onMessageReceived(remoteMessage), can I check to see if the bound Activity is still alive ?

G. Blake Meike
  • 6,615
  • 3
  • 24
  • 40

1 Answers1

2

You can use a WeakReference and DeadObjectException since your Activity seems to be in another app. This will allow you to know if the Activity was garbage collected because your reference will become null and you will not leak.

private WeakReference<MyListener> mListener;

This is how you store the WeakReference.

public void setListener(MyListener listener) 
{
   mListener = new WeakReference<MyListener>(listener);
}  

This is how you use it.

@Override
public void onMessageReceived(RemoteMessage remoteMessage) 
{
    MyListener listener = mListener.get();

    if(listener != null)
    {
        try
        {
            listener.onMessageReceived(remoteMessage);  
        }
        catch(DeadObjectException exception)
        {

        }
    }
    else
    {
        // Activity was destroyed.
    }
}
Newtron Labs
  • 809
  • 1
  • 5
  • 17
  • hmm what if the app crash between MyListener listener = mListener.get(); and listener.onMessageReceived(remoteMessage); ? also as i understand weakreference are deleted when the GC ran, and this is not necessarily at the time the app crash no ? –  Apr 22 '17 at 00:06
  • Once you have the `listener` reference then it cannot be GC. If the app crashes then all will get GC, but yes `WeakReference` are deleted on GC. – Newtron Labs Apr 22 '17 at 00:08
  • yes but is useless to have reference to it if the app is crashed because most probably the code inside listener will crash also of behave unexpected ... –  Apr 22 '17 at 00:20
  • @john since your `Activity` seems to be in another app you can use the `DeadObjectException` to know when the `Activity` is dead. I updated the answer above. – Newtron Labs Apr 22 '17 at 00:25