打开另一个应用程序并传入URL [英] Open another app and pass in url

查看:131
本文介绍了打开另一个应用程序并传入URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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






此代码有效,但会提示您选择用浏览器或谷歌+应用程序打开它

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


解决方案

Application.OpenURL函数无法执行此操作。我以前做过这个并且愿意分享。您可以创建一个Java插件来执行此操作,也可以使用



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


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

解决方案

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:

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.

这篇关于打开另一个应用程序并传入URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆