Xamarin.Android中的ServiceConnection到SystemService(JNI - C ++ - iBinder)

时间:2018-05-22 07:17:36

标签: c# c++ xamarin.android java-native-interface google-project-tango

我正在通过xamarin从经典的Java android应用程序迁移。当通过JNI传递iBinder对象以在本机c ++代码中使用时,我的问题通过Android系统服务使用服务连接相关。更具体地说,我正在尝试连接到我的Asus Zenfone AR上的传统Tango服务。在c ++中访问jobject时,我得到一个巨大的堆栈跟踪,但有以下异常:

art/runtime/java_vm_ext.cc:470] JNI DETECTED ERROR IN APPLICATION: IsInstanceOf received NULL jclass
art/runtime/java_vm_ext.cc:470]     in call to IsInstanceOf
art/runtime/java_vm_ext.cc:470]     from void md50f4250d048afa4418b536451c1ad07e8.TangoServiceConnection.n_onServiceConnected(android.content.ComponentName, android.os.IBinder)

我的代码包括一个BroadcastReceiver,用于控制服务生命周期以及服务连接本身。

public class TangoServiceConnectionReceiver:BroadcastReceiver {         public TangoServiceConnection mTangoServiceConnection = new TangoServiceConnection();

public override void OnReceive(Context context, Intent intent)
{
        if (intent.Action.Equals(MainActivity.TANGO_SERVICE_CONNECT))
        {
            JNITangoWrapper.BindTangoService(context.ApplicationContext, mTangoServiceConnection);
        }
        if (intent.Action.Equals(MainActivity.TANGO_SERVICE_DISCONNECT))
        {
            context.ApplicationContext.UnbindService(mTangoServiceConnection);
            JNITangoNative.OnTangoServiceDisconnected();
        }
    }
};

public class TangoServiceConnection : Java.Lang.Object, IServiceConnection
{
    public void OnServiceConnected(ComponentName name, IBinder service)
    {
        JNITangoNative.OnTangoServiceConnected(JNIEnv.Handle, System.IntPtr.Zero, service.Handle);
    }

    public void OnServiceDisconnected(ComponentName name)
    {
    }
}

JNITangoNative包装器通过JNI传递句柄

[DllImport("library", EntryPoint = "Java_com_SOMEPACKAGE_TangoJNINative_onTangoServiceConnected")]
public static extern void OnTangoServiceConnected(IntPtr jniEnv, IntPtr thiz, IntPtr nativeTangoServiceBinder);

JNIEXPORT void JNICALL
Java_com_SOMEPACKAGE_TangoJNINative_onTangoServiceConnected(
JNIEnv *env, jobject /*caller_object*/, jobject iBinder) {
    app.OnTangoServiceConnected(env, iBinder);
}

在app.OnTangoServiceConnected

void App::OnTangoServiceConnected(JNIEnv *env, jobject iBinder) {
    TangoErrorType ret = TangoService_setBinder(env, iBinder);
    if (ret != TANGO_SUCCESS) {
        LOGE("App: Failed to set Tango binder with error code: %d", ret);
        std::exit(EXIT_SUCCESS);
}

当TangoService_setBinder(env,iBinder)时,应用程序崩溃了已经提供的异常;叫做。我测试了很多我的自我和iBinder jObject的jClass是android.os.BinderProxy,就像在工作的java android app中一样。

我应该再次提到,完全相同的应用程序流程正在我的java android原型中。

我知道这是一项遗留服务,但我需要它以某种方式开发我的应用程序。如果有人能提供额外的信息或提示,我会很高兴,如果需要,可以随时询问更详细的信息。

以下是Visual Studio日志的一些其他输出:

Time    Device Name Type    PID Tag Message
Asus _A002  Info    12143   TangoIHelper    Success! Using default/libtango_client_api.
Asus _A002  Info    6338    TangoCore-UninstallReceiver Tango permissions revoked for Mono.Android.Platform.ApiLevel_24
Asus _A002  Info    6338    TangoCore-UninstallReceiver Uninstall detected.
Asus _A002  Info    6338    TangoCore-UninstallReceiver Tango permissions revoked for com.companyname.Xamarin
Asus _A002  Info    6338    TangoCore-UninstallReceiver Uninstall detected.
Asus _A002  Info    6338    TangoCore-UninstallReceiver Tango permissions revoked for com.companyname.Xamarin
Asus _A002  Info    6338    TangoCore-UninstallReceiver Tango permissions revoked for Mono.Android.DebugRuntime

0 个答案:

没有答案