如何从Unity Android插件调用非静态方法?

时间:2018-09-17 11:47:31

标签: java android unity3d plugins aar

我想使用此Java代码统一获取uri数据 我编写此脚本代码来调用此方法,但不起作用

* java

public String GetUri(){
    Intent intent = getIntent();
    Uri intentData = intent.getData();
    return intentData.getQueryParameter("token");
}

* c#

TextShow.text += ajc.Call<string>("GetUri");

经过多次测试,我发现只有静态方法可以统一调用

像这样

* java

public static String DoSthInAndroid3()
{
    return "33333";
}

* c#

TextShow.text += ajc.Call<string>("DoSthInAndroid3");

我尝试使用非静态方法,但不起作用

public String DoSthInAndroid3()
{
    return "33333";
}

TextShow.text += ajc.Call<string>("DoSthInAndroid3");

如何使用android插件获取uri数据? getIntent()似乎无法在静态方法中调用,如何获取它?

------------ update -----------------

java

package com.s.mylibrary;
import android.net.Uri;
import com.unity3d.player.UnityPlayerActivity;
public class AndroidPlugin extends UnityPlayerActivity {
    public static String GetString(){
        return"TestThisPlugin";
    }

    public String GetUri(){
        Uri intentData = getIntent().getData();
        return intentData.getQueryParameter("token");
    }
}

c#

void GetAndroidUri()
{
    var ajc = new AndroidJavaClass("com.s.mylibrary.AndroidPlugin"); //(1)
    TextShow.text += ajc.CallStatic<string>("GetString");
    TextShow.text += ajc.CallStatic<string>("GetUri");
    TextShow.text += ajc.Call<string>("GetUri");
}

AndroidManifest

      <intent-filter>
        <action android:name="android.intent.action.VIEW"/>
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data android:scheme="justlovepaohuzi" android:host="hmok" android:pathPrefix="/UserToekn"/>
      </intent-filter>

html

        <a href="justlovepaohuzi://hmok/UserToekn/?token=123456999"> Open3 </a>  

----------------- update ---------------

我修复了方法名称并尝试了新的打印代码,我发现我仍然无法获得非静态方法,没有错误消息,但仍然与预期不符

java

package com.s.mylibrary;
import android.net.Uri;
import com.unity3d.player.UnityPlayerActivity;
public class AP extends UnityPlayerActivity{
    public static String GetString(){
        return "TestThisAP";
    }
    public String GetString2(){
        return "AAAAAA";
    }
    public String GetUri(){
        Uri uri = getIntent().getData();
        return uri.getQueryParameter("token")+"XDDX";
    }
}

c#

void GetAndroidUri()
{
    var ajc = new AndroidJavaClass("com.s.mylibrary.AP");
    TextShow.text += ajc.CallStatic<string>("GetString");
    TextShow.text += ajc.Call<string>("GetString2");
    TextShow.text += ajc.Call<string>("GetUri");
    TextShow.text += "Last";
}

随机字符串

预期 “ TestThisAPAAAAAA(令牌)XDDXLast”

结果 “ TestThisAPLast”

1 个答案:

答案 0 :(得分:0)

我终于在本文中找到了解决方案

enter link description here