0

i want to call a method in fragment from service on a message received event.

I have a fcm message service which handles incoming messages. if a certain fragment is showing while message received in service, i want to be able in service to identify wether a certain fragment (for certain user) is showing, if yes a method in fragment should called (which shows message in fragment), if the (user specific) fragment instance is not running, i am generating a notification.

after some research i found code for RemoteCallBack / AIDL, which seems to be perfect, as i can register callback from fragment instance and (on message received) call fragment method from service:

service:

public final RemoteCallbackList<ICallBackAidl> mDMCallbacks = new RemoteCallbackList<ICallBackAidl>();

public void registerDMCallback(ICallBackAidl cb) {
Logger.d(LOG_TAG, "registerDMCallback " + cb);
if (cb != null)
    mDMCallbacks.register(cb);
}

calling method

public void callMehodsInApplication() {
final int N = mDMCallbacks.beginBroadcast();
for (int i = 0; i < N; i++) {
    try {
        mDMCallbacks.getBroadcastItem(i).method1();
    } catch (RemoteException e) {
        e.printStackTrace();
    }
}
mDMCallbacks.finishBroadcast();
}

in fragment:

private ISyncmlServiceDMCallback mCallback = new ISyncmlServiceDMCallback.Stub() {
// Implement callback methods here
public void method1() {
   // Service can call this method
  }
}

public void onServiceConnected(ComponentName name, IBinder service) {   
    svc.LocalBinder binder = (svc.LocalBinder) service;
    mSvc = binder.getService();
    mSvc.registerDMCallback(mCallback);
}

so far things are clear, but now (maybe stupid question):

How do i declare ICallBackAidl? After some research and trail/fail i´m really stuck here.

Norman
  • 33
  • 1
  • 3
  • if you are confused with implementation of interface / callback in aidl for asynchronous callback then refer to answer here : http://stackoverflow.com/a/34217138/3451697 – Harneev Feb 13 '17 at 10:30
  • 1
    As far as I understand in your example `ISyncmlServiceDMCallback` is the same as `ICallBackAidl`. Rename `ICallBackAidl` into `ISyncmlServiceDMCallback` and it should make the job. – OlivierM Sep 27 '17 at 15:16

0 Answers0