其他分享
首页 > 其他分享> > android – StartActivityForResults始终为Intent.ACTION_SEND返回RESULT_CANCELLED

android – StartActivityForResults始终为Intent.ACTION_SEND返回RESULT_CANCELLED

作者:互联网

当出现共享弹出窗口时,我成功分享了WhatsApp上的内容,但仍然返回RESULT_CANCELLED.使用Gmail发送电子邮件时的结果相同.

使用startActivityForResult调用共享意图ACTION_SEND始终返回CANCELED

Intent sharingIntent = new Intent(Intent.ACTION_SEND);
    sharingIntent.setType("text/plain");
    sharingIntent.putExtra(android.content.Intent.EXTRA_TITLE, "Taxeeta, Cab Around The Curb");
    sharingIntent
        .putExtra(
            android.content.Intent.EXTRA_TEXT,
                "Hiring a cab no longer needs you to wait on call centers, or pay a"
                + " convenience (yeah right!!) charge. Taxeeta connects you"
                + " to drivers directly, for a quick book experience. With Taxeeta"
                + " you can take matters in your own hands (literally). To download"
                + " the app for your phone visit http://www.taxeeta.com");
    startActivityForResult(Intent.createChooser(sharingIntent, "Share and earn a extra Priviledge"), 111);

ActivityForResult代码

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 111) {
        if (resultCode == RESULT_OK) {
            Toast.makeText(this, "Ok DUDE", Toast.LENGTH_LONG).show();
        } else if (resultCode == RESULT_CANCELED) {
            Toast.makeText(this, "Oversmart Eh!!", Toast.LENGTH_LONG).show();
        }
    }
}

解决方法:

startActivityForResult()仅适用于要以这种方式调用的活动.如果您调用的活动未显式返回结果,您将获得默认结果RESULT_CANCELED.显然,ACTION_SEND并非旨在以这种方式调用. ACTION_SEND的documentation表示不生成输出(即:不生成结果).

有关Activity.startActivityForResult(),请参阅documentation

Note that this method should only be used with Intent protocols that
are defined to return a result. In other protocols (such as
ACTION_MAIN or ACTION_VIEW), you may not get the result when you
expect. For example, if the activity you are launching uses the
singleTask launch mode, it will not run in your task and thus you will
immediately receive a cancel result.

标签:start-activity,android
来源: https://codeday.me/bug/20190923/1815522.html