【问题标题】:Android: Share plain text using intent (to all messaging apps)Android:使用意图共享纯文本(到所有消息传递应用程序)
【发布时间】:2012-04-14 10:58:30
【问题描述】:

我正在尝试使用意图共享一些文本:

Intent i = new Intent(android.content.Intent.ACTION_SEND);
i.setType("text/plain");  
i.putExtra(android.content.Intent.EXTRA_TEXT, "TEXT");

并使用选择器进行变形:

startActivity(Intent.createChooser(sms, getResources().getString(R.string.share_using)));

有效!但仅适用于电子邮件应用程序。
我需要的是所有消息传递应用程序的一般意图:电子邮件、短信、IM(Whatsapp、Viber、Gmail、SMS...) 尝试使用android.content.Intent.ACTION_VIEW 并尝试使用i.setType("vnd.android-dir/mms-sms"); 没有任何帮助...

"vnd.android-dir/mms-sms" 仅使用短信分享!)

【问题讨论】:

    标签: android email android-intent sms


    【解决方案1】:

    将代码用作:

        /*Create an ACTION_SEND Intent*/
        Intent intent = new Intent(android.content.Intent.ACTION_SEND);
        /*This will be the actual content you wish you share.*/
        String shareBody = "Here is the share content body";
        /*The type of the content is text, obviously.*/
        intent.setType("text/plain");
        /*Applying information Subject and Body.*/
        intent.putExtra(android.content.Intent.EXTRA_SUBJECT, getString(R.string.share_subject));
        intent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
        /*Fire!*/
        startActivity(Intent.createChooser(intent, getString(R.string.share_using)));
    

    【讨论】:

    • 但我不明白有什么不同??只是外面的字符串??
    • 没有区别。在模拟器上,我打开了消息应用程序,但在我的手机和平板电脑上,我被要求从应用程序列表中进行选择。可能是关于在模拟器上安装那些额外的应用程序。
    • 很好的答案!如果您省略 sharingIntent.setType("text/plain"); 部分,谁能说出为什么这不起作用?
    • 如何为 whatsup 设置单独的文本
    • 在intentsharingIntent.setPackage("com.whatsapp")中添加如下sn-p;
    【解决方案2】:

    这样做的新方法是像这样使用 ShareCompat.IntentBuilder:

    // Create and fire off our Intent in one fell swoop
    ShareCompat.IntentBuilder
            // getActivity() or activity field if within Fragment
            .from(this) 
            // The text that will be shared
            .setText(textToShare)
            // most general text sharing MIME type
            .setType("text/plain") 
            .setStream(uriToContentThatMatchesTheArgumentOfSetType)
            /*
             * [OPTIONAL] Designate a URI to share. Your type that 
             * is set above will have to match the type of data
             * that your designating with this URI. Not sure
             * exactly what happens if you don't do that, but 
             * let's not find out.
             * 
             * For example, to share an image, you'd do the following:
             *     File imageFile = ...;
             *     Uri uriToImage = ...; // Convert the File to URI
             *     Intent shareImage = ShareCompat.IntentBuilder.from(activity)
             *       .setType("image/png")
             *       .setStream(uriToImage)
             *       .getIntent();
             */
            .setEmailTo(arrayOfStringEmailAddresses)
            .setEmailTo(singleStringEmailAddress)
            /*
             * [OPTIONAL] Designate the email recipients as an array
             * of Strings or a single String
             */ 
            .setEmailTo(arrayOfStringEmailAddresses)
            .setEmailTo(singleStringEmailAddress)
            /*
             * [OPTIONAL] Designate the email addresses that will be 
             * BCC'd on an email as an array of Strings or a single String
             */ 
            .addEmailBcc(arrayOfStringEmailAddresses)
            .addEmailBcc(singleStringEmailAddress)
            /* 
             * The title of the chooser that the system will show
             * to allow the user to select an app
             */
            .setChooserTitle(yourChooserTitle)
            .startChooser();
    

    如果您对使用 ShareCompat 有任何疑问,我强烈建议您 this great article from Ian Lake,他是 Google 的 Android 开发者倡导者,可以更完整地了解 API。您会注意到,我从那篇文章中借用了一些示例。

    如果这篇文章没有回答您的所有问题,Android 开发者网站上总是有Javadoc itself for ShareCompat.IntentBuilder。我在clemantiano's comment.的基础上在这个API的使用示例中添加了更多内容

    【讨论】:

    • 除了这个答案,还有设置电子邮件地址收件人的方法,如 setEmailBcc()setEmailCc() & setEmailTo().
    • 感谢分享,但它对我来说并不完美,有时我会收到这个异常 java.lang.IllegalArgumentException: Service not registered: ActivityInfo{67f62c5 com.google.android.apps.hangouts.phone. ShareIntentActivity}
    【解决方案3】:

    这是一个关于在 Android 中与 Intents 共享的好例子:

    * Share with Intents in Android

    //Share text:
    
    Intent intent2 = new Intent(); intent2.setAction(Intent.ACTION_SEND);
    intent2.setType("text/plain");
    intent2.putExtra(Intent.EXTRA_TEXT, "Your text here" );  
    startActivity(Intent.createChooser(intent2, "Share via"));
    
    //via Email:
    
    Intent intent2 = new Intent();
    intent2.setAction(Intent.ACTION_SEND);
    intent2.setType("message/rfc822");
    intent2.putExtra(Intent.EXTRA_EMAIL, new String[]{EMAIL1, EMAIL2});
    intent2.putExtra(Intent.EXTRA_SUBJECT, "Email Subject");
    intent2.putExtra(Intent.EXTRA_TEXT, "Your text here" );  
    startActivity(intent2);
    
    //Share Files:
    
    //Image:
    
    boolean isPNG = (path.toLowerCase().endsWith(".png")) ? true : false;
    
    Intent i = new Intent(Intent.ACTION_SEND);
    //Set type of file
    if(isPNG)
    {
        i.setType("image/png");//With png image file or set "image/*" type
    }
    else
    {
        i.setType("image/jpeg");
    }
    
    Uri imgUri = Uri.fromFile(new File(path));//Absolute Path of image
    i.putExtra(Intent.EXTRA_STREAM, imgUri);//Uri of image
    startActivity(Intent.createChooser(i, "Share via"));
    break;
    
    //APK:
    
    File f = new File(path1);
    if(f.exists())
    {
    
       Intent intent2 = new Intent();
       intent2.setAction(Intent.ACTION_SEND);
       intent2.setType("application/vnd.android.package-archive");//APk file type  
       intent2.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(f) );  
       startActivity(Intent.createChooser(intent2, "Share via"));
    }
    break;
    

    【讨论】:

      【解决方案4】:

      使用下面的方法,只需将主题和正文作为参数传递 方法

      public static void shareText(String subject,String body) {
          Intent txtIntent = new Intent(android.content.Intent.ACTION_SEND);
          txtIntent .setType("text/plain");
          txtIntent .putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
          txtIntent .putExtra(android.content.Intent.EXTRA_TEXT, body);
          startActivity(Intent.createChooser(txtIntent ,"Share"));
      }
      

      【讨论】:

        【解决方案5】:

        以下是适用于电子邮件或消息传递应用程序的代码。 如果您通过电子邮件分享,则会添加主题和正文。

        Intent sharingIntent = new Intent(Intent.ACTION_SEND);
                        sharingIntent.setType("text/plain");
        
                        String shareString = Html.fromHtml("Medicine Name:" + medicine_name +
                                "<p>Store Name:" + “store_name “+ "</p>" +
                                "<p>Store Address:" + “store_address” + "</p>")  .toString();
                                              sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Medicine Enquiry");
                        sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareString);
        
                        if (sharingIntent.resolveActivity(context.getPackageManager()) != null)
                            context.startActivity(Intent.createChooser(sharingIntent, "Share using"));
                        else {
                            Toast.makeText(context, "No app found on your phone which can perform this action", Toast.LENGTH_SHORT).show();
                        }
        

        【讨论】:

          【解决方案6】:

          通过使用ACTION_SEND 创建一个Intent,您可以添加额外的类型为Intent.EXTRA_TEXT,第二个参数是您要分享的文本。 然后通过设置分享类型为text/plain,Intent服务会为你带来所有支持分享文字的应用

          Intent sendIntent = new Intent();
          sendIntent.setAction(Intent.ACTION_SEND);
          sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
          sendIntent.setType("text/plain");
          
          Intent shareIntent = Intent.createChooser(sendIntent, null);
          startActivity(shareIntent);
          

          【讨论】:

          • 它只是从文档中复制粘贴。请提供有关这样做的更多信息。
          • @apex39 我这样做了,谢谢
          【解决方案7】:

          图像或二进制数据:

          Intent sharingIntent = new Intent(Intent.ACTION_SEND);
          sharingIntent.setType("image/jpg");
          Uri uri = Uri.fromFile(new File(getFilesDir(), "foo.jpg"));
          sharingIntent.putExtra(Intent.EXTRA_STREAM, uri.toString());
          startActivity(Intent.createChooser(sharingIntent, "Share image using"));
          

          或 HTML:

          Intent sharingIntent = new Intent(Intent.ACTION_SEND);
          sharingIntent.setType("text/html");
          sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, Html.fromHtml("<p>This is the text shared.</p>"));
          startActivity(Intent.createChooser(sharingIntent,"Share using"));
          

          【讨论】:

            【解决方案8】:

            科特林

            在点击侦听器中需要添加此模块,以便通过应用程序(如whatsApp、电子邮件如Gmail、Slack..)共享文本。

            shareOptionClicked.setOnClickListener{
                 val shareText = Intent(Intent.ACTION_SEND)
                 shareText.type = "text/plain"
                 val dataToShare = "Message from my application"
                 shareText.putExtra(Intent.EXTRA_SUBJECT, "Subject from my application")
                 shareText.putExtra(Intent.EXTRA_TEXT, dataToShare)
                 startActivity(Intent.createChooser(shareText, "Share Via"))
                 }
            

            【讨论】:

              【解决方案9】:

              此代码用于通过短信分享

                   String smsBody="Sms Body";
                   Intent sendIntent = new Intent(Intent.ACTION_VIEW);
                   sendIntent.putExtra("sms_body", smsBody);
                   sendIntent.setType("vnd.android-dir/mms-sms");
                   startActivity(sendIntent);
              

              【讨论】:

                【解决方案10】:

                Gmail 共享的 100% 工作代码

                    Intent intent = new Intent (Intent.ACTION_SEND);
                    intent.setType("message/rfc822");
                    intent.putExtra(Intent.EXTRA_EMAIL, new String[]{"anyMail@gmail.com"});
                    intent.putExtra(Intent.EXTRA_SUBJECT, "Any subject if you want");
                    intent.setPackage("com.google.android.gm");
                    if (intent.resolveActivity(getPackageManager())!=null)
                        startActivity(intent);
                    else
                        Toast.makeText(this,"Gmail App is not installed",Toast.LENGTH_SHORT).show();
                

                【讨论】:

                  猜你喜欢
                  • 2018-01-30
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2015-01-25
                  • 1970-01-01
                  • 1970-01-01
                  相关资源
                  最近更新 更多