gpt4 book ai didi

android - Unity 如何调用 Activity 的 onNewIntent()

转载 作者:行者123 更新时间:2023-11-30 02:14:58 24 4
gpt4 key购买 nike

我正在开发 unity android 项目。我已经像这样从统一中调用了 android 端方法

AndroidJavaObject aObj = new AndroidJavaObject("com.mypackage.UnityBridge",param1,param2);
aObj.Call("callme");

Android 端

public class UnityBridge{

public UnityBridge(final String param1, final int param2) {

activity = UnityPlayer.currentActivity;
this.param1= param1;
this.param2= param2;
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
// INITIALIZATION OF ANDROID CLASS CONSTRUCTOR HERE
}
});
}


public void callme() {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
if(obj!= null)
{
// ANDROID METHOD CALL HERE
}
}
});
}

一切正常。

如果我想调用 Activity 特定的方法,如 onPause()onResume(),那么在 unity 中有一个方法可以这样做。

void OnApplicationPause(bool pauseStatus) {
// HERE I CAN CALL activity specific **onPause()** and **onResume()** based on pauseStatus
}

有什么统一的东西可以让我调用 Android 的 onNewIntent(Intent i)。如果不是,那怎么可能调用 onNewIntent()

请帮助解决这个问题。

最佳答案

我写了一篇文章如何在不覆盖 Unity Activity 的情况下解决这个问题(关于 onActivityResult 但原理是一样的):https://medium.com/@tarasleskiv/unity-android-plugins-and-onactivityresult-callback-abef4b6bbc87#.6d7sl8z81

基本上,您创建一个自定义的空 Activity 并启动它只是为了接收这些回调并在之后立即完成。

另请查看这篇关于如何为 onNewIntent 回调创建 Activity 的帖子:https://blog.getsocial.im/android-unity-app-and-the-intent-issue/

这是您必须创建的中间 Activity 的示例代码:

public class MyDeepLinkingActivity extends Activity
{
private static String TAG = "GetSocial GetSocialDeepLinkingActivity";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);

Log.v(TAG, "Forward the intent");

// Do with intent what you need to

Log.v(TAG, "Returning to main activity");
//start main activity
Intent newIntent = new Intent(this, getMainActivityClass());
this.startActivity(newIntent);
finish();
}

private Class<?> getMainActivityClass() {
String packageName = this.getPackageName();
Intent launchIntent = this.getPackageManager().getLaunchIntentForPackage(packageName);
try {
return Class.forName(launchIntent.getComponent().getClassName());
} catch (Exception e) {
Log.e(TAG, "Unable to find Main Activity Class");
return null;
}
}
}

关于android - Unity 如何调用 Activity 的 onNewIntent(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29513296/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com