Unity Android Intents

旧时模样 提交于 2019-12-24 12:13:26

问题


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!


回答1:


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);


来源:https://stackoverflow.com/questions/51460822/unity-android-intents

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!