Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unity Android Intents

I'm trying to launch an android app using Intents in Unity. Using the code I've written, I'm able to open the app, but I'm trying to make it launch into a specific action inside that app.

I found a link on a website that does what I want, but its not in the right format which is: intent://placeID=370731277#Intent;scheme=robloxmobile;package=com.roblox.client;S.browser_fallback_url=https%3A%2F%2Fplay.google.com%2Fstore%2Fapps%2Fdetails%3Fid%3Dcom.roblox.client;end

When I click on this link on the website, it launches the app to the state I want (Specifically, it launches the game with the id 370731277)

I had a look inside the AndroidManifest.xml and found this :

<activity android:name="com.roblox.client.ActivityProtocolLaunch" android:noHistory="true">
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
            <data android:scheme="robloxmobile" />
        </intent-filter>
    </activity>

Which seems to be the activity which the link is running.

My Unity code (C#) is as follows

string data = "robloxmobile://placeID=370731277";
string package = "com.roblox.client";


AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaClass uriClass = new AndroidJavaClass("android.net.Uri");
AndroidJavaObject currentActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
AndroidJavaObject packageManager = currentActivity.Call<AndroidJavaObject>("getPackageManager");

AndroidJavaObject launchIntent = packageManager.Call<AndroidJavaObject>("getLaunchIntentForPackage", package);
AndroidJavaObject uriData = uriClass.CallStatic<AndroidJavaObject>("parse", data);

launchIntent = launchIntent.Call<AndroidJavaObject>("setData", uriData);

currentActivity.Call("startActivity", launchIntent);

This succesfully opens the app, however the game (id 370731277)

I feel like I have everything I need here, I'm just not understanding something - any help would be really appreciated!

like image 740
Ben Walker Avatar asked Jul 12 '26 03:07

Ben Walker


1 Answers

Ended up fixing it - essentially through trial and error, I found I have to be really specific about the activity I want to run. I have to tell it the activity, the action and the scheme + data. This is my new working code.

    string url = "robloxmobile://placeID=370731277";


    AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
    AndroidJavaObject currentActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
    AndroidJavaObject packageManager = currentActivity.Call<AndroidJavaObject>("getPackageManager");

    AndroidJavaClass uriClass = new AndroidJavaClass("android.net.Uri");
    AndroidJavaObject uriData = uriClass.CallStatic<AndroidJavaObject>("parse", url);

    AndroidJavaObject i = packageManager.Call<AndroidJavaObject>("getLaunchIntentForPackage", "com.roblox.client");

    i.Call<AndroidJavaObject>("setClassName", "com.roblox.client", "com.roblox.client.ActivityProtocolLaunch");
    i.Call<AndroidJavaObject>("setAction", "android.intent.action.VIEW");
    i.Call<AndroidJavaObject>("setData", uriData);

    currentActivity.Call("startActivity", i);
like image 93
Ben Walker Avatar answered Jul 13 '26 16:07

Ben Walker



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!