简体   繁体   中英

Launching activity's Intent extra is not updated

I have 2 apps in the same device. One in unity3D and other in 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. How can I do it?

android studio code:

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. 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 ). But we had got problems in the Unity app. 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.

Cheers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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