Reputation: 53
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);
Upvotes: 0
Views: 919
Reputation: 26
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" />
Upvotes: 1
Reputation: 1
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.
Upvotes: 0