I have a service created as follows:
<service android:name="com.myFirebaseMessagingService"> <intent-filter> <action android:name="com.google.firebase.MESSAGING_EVENT"/> </intent-filter> </service>
Then I implement onBind as follows:
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: Activity is attached to the Service and sets the listener. When a service receives a message, it simply starts a listener
Now the big question is: what happens if activity suddenly fails? In this case, mListener will point to something non-existent, no?
How, before calling mListener.onMessageReceived(remoteMessage) , can I check if the associated activity is still maintained?
user7898586
source share