startChooser() 方法和 startActivity() 之间的区别

Difference between startChooser() method and startActivity()

我是 android 的新手,我注意到我可以通过两种方式共享意图。

第一种方式:

ShareCompat.IntentBuilder.from(this). setType(mimeType). setChooserTitle(title). setText(text). startChooser();

第二种方式是:

Intent shareIntent =   ShareCompat.IntentBuilder.from(this)
                                                .setChooserTitle(title)
                                                .setType(mimeType)
                                                .setText(text)
                                                .getIntent();
    if (shareIntent.resolveActivity(getPackageManager()) != null){
        startActivity(shareIntent);
    }

我的问题是,使用 startChooser() 是否可以让我免于使用第二种方式进行检查? ..这两个功能之间还有其他区别吗?

First way :

此处您将 ​​Intent 提供给框架,但您不知道设备中是否有任何应用程序可用于处理该 Intent。

second way :

您首先要确保设备中至​​少有一个可以处理 Intent 的应用程序。

在第二种方式中,您可以在不将该意图传递给框架的情况下处理意外流。比如你可以通知用户 "no application available to handle that action".

Does using startChooser() saves me from the check that i use in the second?

没有

IntentBuilder 基本上是一个帮手 class,用于构建共享意图(Intent#ACTION_SENDIntent#ACTION_SEND_MULTIPLE)并启动活动以共享内容。

also is there any other differences between these two functions

在幕后,startChooser() 和 startActivity() 执行相同的操作。 startChooser() 只是换行 startActivity()。查看 startChooser():

的定义
    /**
     * Start a chooser activity for the current share intent.
     *
     * <p>Note that under most circumstances you should use
     * {@link ShareCompat#configureMenuItem(MenuItem, IntentBuilder)
     *  ShareCompat.configureMenuItem()} to add a Share item to the menu while
     * presenting a detail view of the content to be shared instead
     * of invoking this directly.</p>
     */
     public void startChooser() {
        mActivity.startActivity(createChooserIntent());
     }

我建议您使用第二种方法,它可以让您更好地控制错误情况并向用户提供有意义的信息。