示例#1
0
    void ShowSharingMenuAndroid(Texture2D imageToShare)
    {
        var Title   = "Test Title";
        var AppLink = "Test AppLink";

        var PrioritizedApps = new List <string> {
            "whatsapp", "facebook"
        };

        AN_Intent shareIntent = new AN_Intent();

        shareIntent.SetAction(AN_Intent.ACTION_SEND);

        shareIntent.AddFlags(AN_Intent.FLAG_GRANT_READ_URI_PERMISSION);
        shareIntent.SetType(AN_MIMEDataType.Image);

        string sharingMessageText = "Sharing message text.";

        sharingMessageText = sharingMessageText + "\n\n" + AppLink;

        shareIntent.PutExtra(AN_Intent.EXTRA_TEXT, sharingMessageText);
        shareIntent.PutExtra(AN_Intent.EXTRA_STREAM, imageToShare);

        var pm = AN_MainActivity.Instance.GetPackageManager();
        List <AN_ResolveInfo> resolveInfoList = pm.QueryIntentActivities(shareIntent, 0);

        if (resolveInfoList.Count == 0)
        {
            AN_Logger.Log("Could not find any app for sharing.");

            return;
        }

        List <AN_Intent> intentShareList = new List <AN_Intent>();

        // Try to find apps from priority list to put them on the first positions
        foreach (string appName in PrioritizedApps)
        {
            foreach (AN_ResolveInfo resInfo in resolveInfoList)
            {
                string packageName = resInfo.ActivityInfo.PackageName;
                string name        = resInfo.ActivityInfo.Name;

                if (resInfo.ActivityInfo.PackageName.ToLower().Contains(appName) ||
                    resInfo.ActivityInfo.Name.ToLower().Contains(appName))
                {
                    AN_Intent intent = new AN_Intent(shareIntent);
                    intent.SetPackage(packageName);
                    intentShareList.Add(intent);

                    resolveInfoList.Remove(resInfo);

                    break;
                }
            }
        }

        // Put all the others apps after prioritized ones
        for (int resInfoIndex = 0; resInfoIndex < resolveInfoList.Count; resInfoIndex++)
        {
            AN_ResolveInfo resInfo = resolveInfoList[resInfoIndex];

            string packageName = resInfo.ActivityInfo.PackageName;
            string name        = resInfo.ActivityInfo.Name;

            if (resInfoIndex != resolveInfoList.Count - 1)
            {
                AN_Intent intent = new AN_Intent(shareIntent);
                intent.SetPackage(packageName);
                intentShareList.Add(intent);
            }
            else
            {
                shareIntent.SetPackage(packageName);
            }
        }

        if (intentShareList.Count == 0)
        {
            AN_Logger.Log("Could not find any app for sharing.");
        }
        else
        {
            AN_Logger.Log("proxy.StartActivityForResult");

            AN_Intent chooserIntent = AN_Intent.CreateChooser(shareIntent, Title, intentShareList.ToArray());

            /* AN_ProxyActivity proxy = new AN_ProxyActivity();
             * proxy.StartActivityForResult(chooserIntent, (result) => {
             *   Debug.Log("Unity: Activity Result: " + result.ResultCode.ToString());
             *   proxy.Finish();
             * });*/
            AN_MainActivity.Instance.StartActivity(chooserIntent);
        }
    }