简体   繁体   中英

Intents getExtra()/putExtra in Android Api level 30 not working

Why doesn't this code work fine on devices with Android API level 30 (extras are null)? However with API level 29 the extras are not null and we get what we sent.

The extras we are sending are just one string.

We are launching the intent from Unity app (C#), but not sure if that's relevant.

Java Code in Android Studio

public void onResume() {
    super.onResume();

    
    Bundle extras = getIntent().getExtras();

    if (extras != null) {
        //not null in 29, null in 30
            }
    }

Unity(C#) Code

AndroidJavaClass up = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject ca = up.GetStatic<AndroidJavaObject>("currentActivity");
AndroidJavaObject packageManager = ca.Call<AndroidJavaObject>("getPackageManager");
AndroidJavaObject launchIntent = null;
var bundle = "com.xxxxxx.xxxxx";
launchIntent=packageManager.Call<AndroidJavaObject("getLaunchIntentForPackage",bundle);
launchIntent.Call<AndroidJavaObject>("putExtra", "argument", "StringArgument");
ca.Call("startActivity", launchIntent);

I managed to find a fix for this issue. It seems the newer versions of Android have a separate permission layer for more secure intent action as per this link .

Basically I added this line to both applications' AndroidManifest.xml files (Android Studio app and Unity app):

<uses-permission android:name="android.permission.QUERY_ALL_PACKAGES" tools:ignore="QueryAllPackagesPermission" />

Sorry for the late response. Thought I'd post this since it may still help you or help somebody else.

I also tried to serve data from Unity and had the same issue. Instead of using an intent my solution was to call a method in my Android application. Here's how I did it.

On Unity side

string message = "Hello from Unity!";
AndroidJavaClass bridgeClass = new AndroidJavaClass("com.example.BridgeActivity");
bridgeClass.CallStatic("unityDataServed", message);

On Android side

Create a static method (in this example, the method is called unityDataServed) that receives a string as parameter in the class referenced by bridgeClass.

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