打开另一个应用程序并传入URL

时间:2017-01-18 23:22:58

标签: c# android ios unity3d

当我点击按钮时,我试图团结一致打开Goog​​le+应用。 我在我的Android设备上运行该应用程序,但它无法正常工作 这是我用的代码

Application.OpenURL ("gplus://plus.google.com/+JewelMash");

此代码有效,但它会提示您选择使用浏览器或google + app打开它

Application.OpenURL ("https://plus.google.com/+JewelMash");

1 个答案:

答案 0 :(得分:3)

Application.OpenURL函数无法执行此操作。我以前做过这个并且愿意分享。您可以创建一个Java插件来执行此操作,也可以使用AndroidJavaClass和C#来执行此操作。我在C#中做了我的,因为这是一个简单的插件。

打开任何应用程序:

public bool openUrl(string packageName)
{
    #if UNITY_ANDROID
    AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
    AndroidJavaObject unityActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
    AndroidJavaObject pManager = unityActivity.Call<AndroidJavaObject>("getPackageManager");

    AndroidJavaObject intent = null;
    try
    {
        intent = pManager.Call<AndroidJavaObject>("getLaunchIntentForPackage", packageName);
        unityActivity.Call("startActivity", intent);
        return true;
    }
    catch (Exception e)
    {
        Debug.LogWarning("Failed to Opeen App: " + e.Message);
        //Open with Browser
        string link = "https://play.google.com/store/apps/details?id=" + packageName + "&hl=en";

        Application.OpenURL(link);
        return false;
    }
    #endif
    return false;
}

<强>用法

bool success = openUrl("com.google.android.apps.plus");

要打开另一个应用,然后传入Url:

public bool openUrl(string packageName, string url)
{
    #if UNITY_ANDROID
    AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
    AndroidJavaObject unityActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
    AndroidJavaObject pManager = unityActivity.Call<AndroidJavaObject>("getPackageManager");

    //For accessing static strings(ACTION_VIEW) from android.content.Intent
    AndroidJavaClass intentStaticClass = new AndroidJavaClass("android.content.Intent");
    string actionView = intentStaticClass.GetStatic<string>("ACTION_VIEW");

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

    //Psss ACTION_VIEW and Uri.parse to the intent
    AndroidJavaObject intent = new AndroidJavaObject("android.content.Intent", actionView, uriObject);

    try
    {
        if (pManager.Call<AndroidJavaObject>("getPackageInfo", packageName, 0) != null)
        {
            intent.Call<AndroidJavaObject>("setPackage", packageName);
        }
    }
    catch (Exception e)
    {
        Debug.LogWarning("Failed to Open App 1: " + e.Message);
        return false;
    }

    try
    {
        unityActivity.Call("startActivity", intent);
        return true;
    }
    catch (Exception e)
    {
        Debug.LogWarning("Failed to Open App 2: " + e.Message);
        //Open with Browser
        string link = "https://play.google.com/store/apps/details?id=" + packageName + "&hl=en";

        Application.OpenURL(link);
        return false;
    }
    #endif
    return false;
}

<强>用法

 bool success = openUrl("com.google.android.apps.plus", "https://plus.google.com/+JewelMash");

您正在寻找第二个代码。如果要查找其他应用程序的程序包名称,请使用以下图像:

enter image description here

此解决方案适用于Android。您将需要Object-C插件才能在iOS中执行此操作。我建议你在iOS上提出一个关于如何做到这一点的新问题。