Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
131 views
in Technique[技术] by (71.8m points)

c# - Open another app and pass in url

I'm trying on unity to open the Google+ app when I click on a button. I run the app on my android device but it's not working this is the code I used

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

this code works but it prompts you to choose to open it with a browser or google + app

Application.OpenURL ("https://plus.google.com/+JewelMash");
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The Application.OpenURL function cannot do this. I've done this before and willing to share. You can make a Java plugin to do this or you can use AndroidJavaClass and C# to do it. I did mine in C# since this is a simple plugin.

To Open any App:

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

Usage:

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

To Open another App then pass in 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;
}

Usage:

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

You are looking for the second code. If you want to find the package name of another app use the image below:

enter image description here

This solution is for Android. You will need Object-C plugin to do this in iOS. I suggest you ask a new question about how to do this in iOS.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...