Question

Having trouble making my Android-app receive RemObjects-events...

Here's the Android (java) class:

package no.wolftech.fieldagentforandroid.tools;

import java.net.URI;
import java.util.Date;

import no.wolftech.fieldagentforandroid.AndroidService_AsyncProxy;
import no.wolftech.fieldagentforandroid.FieldAgent;
import no.wolftech.fieldagentforandroid.MonitorService;
import no.wolftech.fieldagentforandroid.OnIdleReportDeliveredEvent;
import no.wolftech.fieldagentforandroid.R;
import no.wolftech.fieldagentforandroid.ROEventsAdapter;
import no.wolftech.fieldagentforandroid.ReportStruct;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.util.Log;

import com.remobjects.sdk.AsyncRequest;
import com.remobjects.sdk.ClientChannel;
import com.remobjects.sdk.ClientChannel.IClientChannelCallback;
import com.remobjects.sdk.EventNotifier;
import com.remobjects.sdk.EventReceiver;

public class AndroidService
{
    final AndroidService_AsyncProxy androidService;
    final EventReceiver receiver;
    EventNotifier notifier;
    private Handler aHandler;
    Context context;
    URI serverURI;
    Boolean result;

    public AndroidService()
    {
        androidService = SetupAndroidService();
        aHandler = new Handler();
        receiver = SetupEventReceiver();


        /*androidService.beginSubscribeToEvents(true, new AsyncRequest.IAsyncRequestCallback()
        {
            @Override
            public void completed(AsyncRequest aRequest)
            {
                androidService.endSubscribeToEvents(aRequest);
                Log.d("AndroidService", "Subscribed to Events");
            }
            @Override
            public void failed(AsyncRequest aRequest, Exception aException) 
            {
                Log.e("AndroidService", "SubscribeToEvents Failed: " + aException.getMessage());    
            }
        });*/
    }

    public AndroidService_AsyncProxy SetupAndroidService()
    {
        try
        {
            context = FieldAgent.getAppContext();
            serverURI = new URI(context.getString(R.string.server_adress));
        }
        catch (Exception e)
        {
            Log.e("AndroidService", e.getMessage());
        }
        final AndroidService_AsyncProxy androidService = new AndroidService_AsyncProxy(serverURI);

        // Set channel callback
        androidService.getProxyClientChannel().setChannelCallback(new IClientChannelCallback() {
          @Override
          public void requestFailWithException(Exception aException) {
            Log.e("AndroidService", "Exception : " + aException.getMessage());
          }
          @Override
          public boolean clientChannelNeedsLogin(Exception aException) {
              Log.e("AndroidService", "clientChannelNeedsLogin: " + aException.getMessage());

              return false;
          }
        });
        Log.d("AndroidService", "AndroidService Initiated");
        return androidService;
    }

    public EventReceiver SetupEventReceiver()
    {
        EventReceiver receiver = new EventReceiver(androidService, "AndroidServiceReceiver");
        receiver.setChannel(ClientChannel.channelWithURI(serverURI));
        receiver.setServiceName(androidService._getInterfaceName());
        receiver.setMinPollInterval(1);
        receiver.setMaxPollInterval(3);

        receiver.addListener(new ROEventsAdapter() {
            @Override
            public void OnIdleReportDelivered(final OnIdleReportDeliveredEvent aEvent)
            {
                Log.d("AndroidServiceEventReceiver", "OnIdleReportDelivered");

                aHandler.post(new Runnable()
                {
                    @Override
                    public void run()
                    {
                        ReportStruct report = aEvent.get_DeliveredReport();

                        FieldAgent.setLastReportTime("Last Report Sent " + report.getTime());

                        Log.d("AndroidServiceEventReceiver", "Stopping MonitorService");
                        Intent serviceIntent = new Intent(FieldAgent.getAppContext(),
                            MonitorService.class);
                        FieldAgent.getAppContext().stopService(serviceIntent);

                        FieldAgent.setErrorState(FieldAgent.ERRORSTATE_NO_ERROR);
                    }
                });
            }
        });
        Log.d("AndroidService", "EventReceiver Initiated");
        return receiver;
    }

    private void setResult(Boolean inResult)
    {
        result = inResult;
    }

    private Boolean getResult()
    {
        return result;
    }

    public Boolean SendIdleReport(double latitude, double longitude)
    {
        Date now = new Date();

        androidService.beginAddIdleReport(now, longitude, latitude, "Android Location", true, new AsyncRequest.IAsyncRequestCallback()
        {
            @Override
            public void completed(AsyncRequest aRequest)
            {
                androidService.endAddIdleReport(aRequest);
                Log.d("AndroidService", "IdleReport sent");
                setResult(true);
            }
            @Override
            public void failed(AsyncRequest aRequest, Exception aException) 
            {
                Log.e("AndroidService", "AddIdleReport Failed: " + aException.getMessage());    
                setResult(false);
            }
        });
        return getResult();
    }
}

And here's how I'm subscribing to the events on the server:

IAndroidEvents ev = (IAndroidEvents)GetEventSink(typeof(IAndroidEvents), new Guid[] { SessionID });

I've also tried using a subscribe-method like this:

public virtual bool SubscribeToEvents()
{
    try
    {
        Guid currentSession = SessionID;

        EventSinkManager.SubscribeClient(currentSession, typeof(IAndroidEvents));

        return true;
    }
    catch (Exception e)
    {
        return false;
    }
}

But no luck. The channel-callback (ClientChannelNeedsLogin) works, SendIdleReport() works, but no events... Can anyone help?

Was it helpful?

Solution

I figured it out, I had forgotten this very important little line:

receiver.start()

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top