【问题标题】:How can I detect if user selected from createChooser options?如何检测用户是否从 createChooser 选项中选择?
【发布时间】:2010-07-20 08:25:49
【问题描述】:

我有一个代码会提示用户通过选择所需的应用程序来发送消息,我如何检测用户是否真的从选项中进行了选择,还是按下了返回?

我尝试检查 Intent 是否返回了某些内容,但正在异步运行,因此无法跟踪。

我还尝试使用startActivityForResult 运行意图,我在 onActivityResult 中注意到 resultCode 始终为 0(RESULT_CANCELED),即使用户选择了,或者不是来自选择器。

【问题讨论】:

    标签: android


    【解决方案1】:

    从Android的源码可以看出,在Intents中选择的Activity根本没有setResult()。这应该作为一项功能提出要求。

    【讨论】:

    • 有没有知道他们选择了哪个应用程序?
    【解决方案2】:

    您现在可以使用新的 createChooser() 和第三个参数:pendingintent.getintentsender()。

    示例:

            String aText = e.getText().toString();
            Intent sendIntent = new Intent();
            sendIntent.setAction(Intent.ACTION_SEND);
            sendIntent.putExtra(Intent.EXTRA_TEXT, aText);
            sendIntent.setType("text/plain");
            sendIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            sendIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
    
            //
            Intent receiver = new Intent(this, BroadcastTest.class);
            receiver.putExtra("test", "test");
            PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, receiver, PendingIntent.FLAG_UPDATE_CURRENT);
            Intent intent = Intent.createChooser(sendIntent, "Send email with:", pendingIntent.getIntentSender());
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
    

    【讨论】:

      猜你喜欢
      • 2016-10-31
      • 2018-05-05
      • 1970-01-01
      • 2019-02-25
      • 1970-01-01
      • 1970-01-01
      • 2011-01-24
      • 1970-01-01
      • 2013-03-17
      相关资源
      最近更新 更多