c# - 如何在 Android 上接收 RemObjects 事件?

标签 c# java android events remobjects

让我的 Android 应用程序接收 RemObjects 事件时遇到问题...

这是 Android (java) 类:

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();
    }
}

这是我订阅服务器上的事件的方式:

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

我也尝试过使用这样的订阅方法:

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

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

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

但运气不佳。 channel 回调 (ClientChannelNeedsLogin) 有效,SendIdleReport() 有效,但没有事件...任何人都可以帮忙吗?

最佳答案

我发现了,我忘记了这一非常重要的小行:

接收器.start()

关于c# - 如何在 Android 上接收 RemObjects 事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17615175/

相关文章:

c# - 我应该对这个类进行单元测试吗?

c# - 如何在 .net v1.3 的 spring 中加密属性文件中的值

java - 错误java : cannot find symbol when building with JHipster

java - Hibernate 3.6.10 不通过 OneToMany JoinTable 级联删除

java - 如何在 LibGdx 核心中使用 Android 方法

Android:在 Webview 上播放 youtube 视频

c# - 为什么 'Remember Me' 在 VS2013 的 ASP.NET MVC 5 样板代码中不起作用?

java - 有没有 'standard way'来解析关于:blank URL's in Java?

android - Android 中 WebView 底部的空白

c# - 在 asp.net 中定义查询字符串