如何使用ShareCompat.IntentBuilder启动活动

use*_*698 9 android android-appcompat android-intent

我正在尝试添加共享意图以发布到Google Plus,似乎无法解决将新ShareCompat.IntentBuilder(Android支持库类)传递给该startActivity方法的问题.我开始使用这个例子.我的应用程序是使用Android 2.2平台编译的.是否有可能有另一种支持方式来启动活动以启动共享意图.

IntentBuilder shareIntent = ShareCompat.IntentBuilder.from(MyActivity.this);                
shareIntent.setText(message);
shareIntent.setSubject(subject);

if (mFullFileName != null) {
    File imageFile = new File(mFullFileName);
    if (imageFile.exists()) {
        shareIntent.setStream(Uri.fromFile(imageFile));
        shareIntent.setType("image/jpeg");
    }
} else {
    shareIntent.setType("*.*");
}   
shareIntent.getIntent();
// doesn't compile only accepts Intent and not the Intentbuilder 
startActivity(shareIntent); 
Run Code Online (Sandbox Code Playgroud)

kle*_*nki 18

这是我的代码中的一个示例,但如果您想要一些参考资料,请点击文章的超链接.

public void shareText(String text) {
        String mimeType = "text/plain";
        String title = "Example title";

        Intent shareIntent =   ShareCompat.IntentBuilder.from(this)
                                                    .setType(mimeType)
                                                    .setText(text)
                                                    .getIntent();
        if (shareIntent.resolveActivity(getPackageManager()) != null){
            startActivity(shareIntent);
        }
    }
Run Code Online (Sandbox Code Playgroud)

ShareCompat.IntentBuilder上的博客文章和分享意图

  • 仅供参考,您可以用“getIntent”替换“createChooserIntent”来强制选择器。这样,您就不必检查包管理器是否能够解析该活动,因为选择器将处理该问题。 (2认同)
  • 你在哪里使用了标题? (2认同)

Ahm*_*aad 16

ShareCompat.IntentBuilder.from(ActivityName.this)弃用,请像这样使用 IntentBuilder 的构造函数:

科特林:

    ShareCompat
               .IntentBuilder(this@YourActivity)
               .setType("text/plain")
               .setChooserTitle("Share text with: ")
               .setText("Desired text to share")
               .startChooser()
Run Code Online (Sandbox Code Playgroud)

爪哇:

new ShareCompat
                .IntentBuilder(YourActivity.this)
                .setType("text/plain")
                .setChooserTitle("Share text with: ")
                .setText("Desired text to share")
                .startChooser();
Run Code Online (Sandbox Code Playgroud)


use*_*698 8

有趣的是,我只是想通了......给出的例子是假设创建一个Intent而不是一个IntentBuilder对象..必须改变我的代码来链接对象的创建.

Intent i = ShareCompat.IntentBuilder.from(MyActivity.this)
                       .setText(message)
                       .setSubject(subject)
                       .setStream(Uri.fromFile(imageFile))
                       .setType("image/jpeg")
                       .getIntent()
                       .setPackage("com.google.android.apps.plus");
Run Code Online (Sandbox Code Playgroud)