Unity Android意图 [英] Unity Android Intents
问题描述
我正在尝试使用Unity中的Intent启动Android应用.使用我编写的代码,我可以打开该应用程序,但是我试图使其在该应用程序内的特定操作中启动.
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.
我在网站上找到了符合我想要的链接,但链接格式不正确
这是:
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
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
当我单击网站上的此链接时,它将启动应用程序到我想要的状态(具体来说,它将启动ID为370731277的游戏)
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)
我查看了AndroidManifest.xml并发现了这个内容:
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.
我的Unity代码(C#)如下
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);
这成功打开了应用程序,但是却打开了游戏(id 370731277)
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!
推荐答案
最终修复了该问题-本质上是通过反复试验,我发现我必须对要运行的活动非常具体.我必须告诉它活动,动作和方案+数据.这是我的新工作代码.
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);
这篇关于Unity Android意图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!