gpt4 book ai didi

android - 无法在 Unity Activity 中返回结果

转载 作者:行者123 更新时间:2023-11-30 00:02:22 25 4
gpt4 key购买 nike

我遇到了以下问题,

我有一个托管我的 UnityApplication 的应用程序,它是通过 startActivityForResult 从我的 MainApplication 调用的。我在调用 mUnityPlayer.quit 之前设置了 Result,但是当返回到主应用程序时,数据 Intent 始终为 null。

onDestroy部分的代码:

 @Override protected void onDestroy ()
{
Log.d(this.getClass().getSimpleName(),"UnityApp.onDestroy called");
closeSession();
Log.d(this.getClass().getSimpleName(),"UnityApp.onDestroy: after closeSession");
//finish();
setResult(resultCode,resultData);
Log.d(this.getClass().getSimpleName(),"UnityApp.onDestroy: Result set 1");
UnityPlayer.currentActivity.setResult(resultCode,resultData);
Log.d(this.getClass().getSimpleName(),"UnityApp.onDestroy: Result set 2");
this.mUnityPlayer.quit();
super.onDestroy();
Log.d(this.getClass().getSimpleName(),"UnityApp.onDestroy: after super.onDestroy");
Log.d(this.getClass().getSimpleName(),"UnityApp.onDestroy: after mUnityPlayer.quite");
UnityPlayer.currentActivity.finish();
Log.d(this.getClass().getSimpleName(),"UnityApp.onDestroy after finish");
}

调用 Activity 代码:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CALL_EXTERN_APP) {
Log.d(this.getClass().getSimpleName(), "ListFiles:");
txtHelloWorld.setText("data==null");
if(data==null) return;
txtHelloWorld.setText("no Filename");
if(!data.hasExtra(EXTRA_SESSION_FILENAME)) return;
txtHelloWorld.setText("no Filecontent");
if(!data.hasExtra(EXTRA_SESSION_FILECONTENT))return;
txtHelloWorld.setText("Successful");
if (resultCode == 0 && callSuccess) {
String filename = data.getStringExtra(EXTRA_SESSION_FILENAME);
String[] filecontent = data.getStringArrayExtra(EXTRA_SESSION_FILECONTENT);
File file = new File(getFilesDir(),filename);
Log.d(this.getClass().getSimpleName(), "onActivityResult:" + filecontent.length + " Lines to write to " + filename);
try {
if(!file.exists())
file.createNewFile();
FileOutputStream fout = new FileOutputStream(file);
for (int l = 0; l < filecontent.length; l++) {
fout.write(filecontent[l].getBytes());
Log.d(this.getClass().getSimpleName(), "onActivityResult:" + "Line " + (l + 1) + ": " + filecontent[l]);
}
fout.close();
Log.d(this.getClass().getSimpleName(), "onActivityResult: Write to " + filename + " done");
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

我做错了什么吗?或者是否有更好的解决方案将数据从 Extern Unity 应用程序返回到我的主应用程序?

最佳答案

在 onDestroy() 中执行这段代码并不是一个好主意。您应该将代码移动到调用 finish() 的地方。这将确保您的代码可靠地工作并确保正确调用 onActivityResult()

关于android - 无法在 Unity Activity 中返回结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49693166/

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