【问题标题】:Default mail client continues to be running in background after mail is sent默认邮件客户端在发送邮件后继续在后台运行
【发布时间】:2013-06-21 15:08:04
【问题描述】:

我正在使用 ACTION_SEND 从我的应用程序发送邮件。它工作正常,但问题是,由于我使用 Intent.FLAG_ACTIVITY_NEW_TASK 标志来发送邮件,即使在发送邮件之后,电子邮件客户端也会继续在后台运行。最糟糕的是它仍然将我的电子邮件显示为草稿(尚未发送)。

我使用下面的代码从非活动中发送邮件

    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType("message/rfc822");
    intent.putExtra(Intent.EXTRA_SUBJECT, "Report issue: \""+mIssueTitle+"\"");
    intent.putExtra(Intent.EXTRA_TEXT, mailBody);       
    String[] mailIds = new String[] {getReportingMailId()};
    intent.putExtra(Intent.EXTRA_EMAIL, mailIds);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    App.getContext().startActivity( intent );

如果邮件客户端永远在后台运行,我如何触发邮件客户端从非活动中发送邮件

【问题讨论】:

    标签: android email


    【解决方案1】:

    好的,终于找到解决方案了。

    问题是,我正在使用 Application#Context 来启动活动。每当我们使用 Application#Context 时,都会创建一个新任务(这就是为什么,如果我们不添加此标志,则在意图中添加标志 Intent.FLAG_ACTIVITY_NEW_TASK 会导致应用崩溃)。

    为了避免创建新任务,我们需要做的就是使用正确的上下文,即使用活动上下文。 (“在存储活动上下文引用的情况下要小心并避免内存泄漏”)。所以代码如下

    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType("message/rfc822");
    intent.putExtra(Intent.EXTRA_SUBJECT, "Report issue: \""+mIssueTitle+"\"");
    intent.putExtra(Intent.EXTRA_TEXT, mailBody);       
    String[] mailIds = new String[] {getReportingMailId()};
    intent.putExtra(Intent.EXTRA_EMAIL, mailIds);
    
    enclosingActivityContext.startActivity( intent );
    

    【讨论】:

      猜你喜欢
      • 2014-07-11
      • 2010-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-19
      • 1970-01-01
      • 1970-01-01
      • 2018-10-19
      相关资源
      最近更新 更多