简体   繁体   English

启动活动的Intent Extra未更新

[英]Launching activity's Intent extra is not updated

I have 2 apps in the same device. 我在同一设备上有2个应用。 One in unity3D and other in android studio. 一个在unity3D中,另一个在android studio中。 My unity code is not getting updated intent extras instead it gets the intent extras of the intent which initially launched the app. 我的统一代码没有获得更新的意图附加功能,而是获得了最初启动该应用程序的意图的附加功能。 If the unity app has been running on background the intent extra in unity never get updated. 如果统一应用程序已在后台运行,则永远不会更新统一意图。

I want to get the updated intent.extra from android app to unity app. 我想将更新的intent.extra从android应用程序更新为unity应用程序。 How can I do it? 我该怎么做?

android studio code: android studio代码:

public class MainActivity extends AppCompatActivity {
public void sendMessage(View view) {
      getPackageManager().getLaunchIntentForPackage("com.amali.UnityApp").setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        Intent launchIntent = getPackageManager().getLaunchIntentForPackage("com.amali.UnityApp");
        if (launchIntent != null) {

            launchIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |Intent.FLAG_ACTIVITY_NEW_TASK );
            String s = "hello " + incrementedstring;   //updating the string passing to unity app
            launchIntent.putExtra("arguments", s);
            Bundle extras= launchIntent.getExtras();
            Log.d(TAG, extras.getString("arguments"));  //this prints correctly
            startActivity(launchIntent);
        }

} }

unity code: 统一代码:

void GetAndroidAppArguments()
{
    string arguments = "";
    try
    {
        AndroidJavaClass UnityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
        AndroidJavaObject currentActivity = UnityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
        AndroidJavaObject intent = currentActivity.Call<AndroidJavaObject>("getIntent");
        bool hasExtra = intent.Call<bool>("hasExtra", "arguments");
        if (hasExtra)
        {
            AndroidJavaObject extras = intent.Call<AndroidJavaObject>("getExtras");
            arguments = extras.Call<string>("getString", "arguments");
            debugLog.text = arguments;
        }
    }
    catch (Exception ex)
    {
        debugLog.text = ex.Message;
    }
}

It seems like intent's extras never get updated. 似乎intent的附加功能永远不会更新。 I am struggling with this for weeks. 我为此奋斗了数周。 Please help me. 请帮我。

I cannot answer well the Android App because we used Cordova with a Plugin to send the extras (a var called varName ). 我无法很好地回答Android App,因为我们使用了带有插件的Cordova发送额外内容(称为varName的var)。 But we had got problems in the Unity app. 但是我们在Unity应用程序中遇到了问题。 In our case this works perfectly: 在我们的情况下,这很完美:

AndroidJavaClass UnityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); 

 AndroidJavaObject currentActivity = UnityPlayer.GetStatic<AndroidJavaObject>("currentActivity");

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

 String text = intent.Call<String> ("getStringExtra", "varName");

The thing was that we tried your code, but we didn't get anything. 问题是我们尝试了您的代码,但没有得到任何结果。 After a while, we notice that Call calls a Java method, so we try the getStringExtra method to get the specific value. 一段时间后,我们注意到Call调用了Java方法,因此我们尝试使用getStringExtra方法来获取特定值。

Cheers. 干杯。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM