Android getintent is returning null

Hi,

I am trying to get the url for the intent. I am trying to do getIntent().getData() in unity.

The intent is always null, so the call intent.Call(“getData”); always fails.

There are other threads about that but they are all old and the problem is a little different, so I think now there should be an easy way to make getIntent().getData().

AndroidJavaClass unityPlayerClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
        activityObject = unityPlayerClass.GetStatic<AndroidJavaObject>("currentActivity");

        AndroidJavaObject intent = activityObject.Call<AndroidJavaObject>("getIntent");

        intentData = intent.Call<AndroidJavaObject>("getData");

Any help is greatly appreciated.

According to the documentation of getData (see link here)

This means that this method can return null. AFAIK, there’s no way to represent a null response from a method using Unity’s AndroidJavaObject or AndroidJavaClass - in case a null is returned, the code will throw an exception (you cannot initialize a AndroidJavaObject class with a null reference).
You can see this also when peeking into the constructor of this class:
2298734--154720--upload_2015-9-17_0-34-59.png

Before trying to solve this particular scenario, what are you trying to achieve exactly ?

From what i can think of, you have 2 options:

  • Create your own .JAR library with your own methods. These will call getIntent() and getData(), do all the null checks and return the proper data (never return null).
  • Skip AndroidJavaObject and work at the AndroidJNI classes layer - create your own JNI class definitions and method signatures, and invoke them to retrieve an IntPtr response (then you can check if it’s null or not).

Frankly i think that option #1 is better and much easier (create a simple JAR with a few utility methods and invoke those from Unity). I’ve never tried option #2 (it should work only in theory, but involves a bunch of hackery using reflection, since most of what you need is not exposed as public).

I may be wrong and there may be other options here, maybe @Yury-Habets can advise :slight_smile:

1 Like

@liortal
Thank you for your detailed reply.
Now I know that the exception I get is because getData returns null.
I will give option 1 and 2 a try.

I am using a native library where I have to call a function passing the getIntent()->getData() parameter to it.
I thought it’s easier to call it from inside c#.

I solved it using the following code.

       AndroidJavaObject intent = activityObject.Call<AndroidJavaObject>("getIntent");
        System.IntPtr method_getData = AndroidJNIHelper.GetMethodID(intent.GetRawClass(), "getData",
                "()Ljava/lang/Object;");
        System.IntPtr getDataResult = AndroidJNI.CallObjectMethod(activityObject.GetRawObject(),
                                    method_getData, AndroidJNIHelper.CreateJNIArgArray(new object[0]));

Thanks every one!

2 Likes

Does this approach still work for you in Unity 5.6?
I tried it and it just immediately crashes the app.

can you post the log from the crash ?

I got the same problem. I think it’s a bug in the code posted above. activityObject.GetRawObject() should be intent.GetRawObject() so the code should read;

  AndroidJavaObject intent = activityObject.Call<AndroidJavaObject>("getIntent");
        System.IntPtr method_getData = AndroidJNIHelper.GetMethodID(intent.GetRawClass(), "getData",
                "()Ljava/lang/Object;");
        System.IntPtr getDataResult = AndroidJNI.CallObjectMethod(intent.GetRawObject(),
                                    method_getData, AndroidJNIHelper.CreateJNIArgArray(new object[0]));