通过共享选项列表检索共享文本?

时间:2018-04-27 19:05:19

标签: c# android xamarin xamarin.forms visual-studio-2017

如何检索通过android中的共享选项列表共享的文本? 这是我正在使用的代码。我基本上通过从谷歌中选择文本来共享文本到我的应用程序,然后右键单击它并单击共享,我在那里选择我的应用程序。共享功能工作得很好,但我想检索我想要分享的代码中的实际文本!

[Activity(Label = "TextRecieve", Icon = "@drawable/ic_home", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
[IntentFilter(new[] { Intent.ActionSend }, Categories = new[] { Intent.CategoryDefault }, DataMimeType = "image/*", Label = "TextRecieve")]
[IntentFilter(new[] { Intent.ActionSend }, Categories = new[] { Intent.CategoryDefault }, DataMimeType = "text/plain", Label = "TextRecieve")]
[IntentFilter(new[] { Intent.ActionSendMultiple }, Categories = new[] { Intent.CategoryDefault }, DataMimeType = "image/*", Label = "TextRecieve")]


        Intent intent = Intent;
        String action = Intent.Action;
        String type = intent.Type;

        if (Intent.ActionSend.Equals(action) && type != null)
        {
            if ("text/plain".Equals(type))
            {
                // Handle text being sent
                // ...
                // ...
                // ...     

            }
            else if (type.StartsWith("image/"))
            {
                // Handle single image being sent
                // ...
                // ...
                // ...    
            }
        }
        else if (Intent.ActionSendMultiple.Equals(action) && type != null)
        {
            if (type.StartsWith("image/"))
            {
                // Handle multiple images being sent
                // ...
                // ...
                // ...                        
            }
        }
        else
        {
            // Handle other intents, such as being started from the home screen                    
        }

1 个答案:

答案 0 :(得分:0)

您应该在“text / plain”语句中添加它。这将通过指定的Intent“intent”将您正在共享的文本提取到您的应用程序。

String sharedText = intent.GetStringExtra(Intent.ExtraText);

总代码

        Intent intent = Intent;
        String action = Intent.Action;
        String type = intent.Type;

        if (Intent.ActionSend.Equals(action) && type != null)
        {
            if ("text/plain".Equals(type))
            {
                // Handle text being sent
                // ...
                // ...
                // ...     
                String sharedText = intent.GetStringExtra(Intent.ExtraText);
            }
            else if (type.StartsWith("image/"))
            {
                // Handle single image being sent
                // ...
                // ...
                // ...    
            }
        }
        else if (Intent.ActionSendMultiple.Equals(action) && type != null)
        {
            if (type.StartsWith("image/"))
            {
                // Handle multiple images being sent
                // ...
                // ...
                // ...                        
            }
        }
        else
        {
            // Handle other intents, such as being started from the home screen                    
        }