android-activity - Application 子类中的 startActivity

标签 android-activity android

我有一个小的 Android 应用程序,我在其中直接指定我的应用程序并在 ApplicationSubclass 的 onCreate 中进行一些应用程序范围的设置,但是我收到以下错误(注意,我知道 FLAG_ACTIVITY_NEW_TASK):

Caused by: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity  context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
    at android.app.ContextImpl.startActivity(ContextImpl.java:644)
    at android.content.ContextWrapper.startActivity(ContextWrapper.java:258)
    at somePart.ofA.nameSpace.ApplicationSubclass.sendNotificationEmail(ApplicationSubclass.java:186)

当抛出一些异常并捕获它们时,我会调用此 sendNotificationEmail,以便应用程序的用户可以发送一封包含异常信息的小电子邮件,这样我就可以更轻松地修复可能出现的任何问题或指导他们解决问题。

这里是一些相关的代码:

list .xml:

<application android:label="@string/app_name" android:icon="@drawable/icon" android:name=".ApplicationSubclass">
// ... some stuff, including all of my app's activities
</application>

ApplicationSubclass 定义为:

public class ApplicationSubclass extends Application {
   // Multiple methods, including an override of onCreate

  public void sendNotificationEmail(String emailBody) {
      Intent emailIntent = new Intent(Intent.ACTION_SEND);
      emailIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
      emailIntent.setType("text/html");
      emailIntent.putExtra(Intent.EXTRA_EMAIL, notificationRecipients);
      emailIntent.putExtra(Intent.EXTRA_SUBJECT, "MyAppName Error");
      emailIntent.putExtra(Intent.EXTRA_TEXT, emailBody);
      try {
          startActivity(Intent.createChooser(emailIntent, "An error has occurred! Send an error report?"));
      } catch (ActivityNotFoundException e) {
          // If there is nothing that can send a text/html MIME type
          e.printStackTrace();
      }
   }
}

我想知道为什么会这样。我阅读了一些文档,在 StackOverflow 和整个互联网上寻找一些答案。在我看来,我应该没问题,因为我正在设置 FLAG_ACTIVITY_NEW_TASK,但事实显然并非如此!

仅仅是因为我什至不能尝试从应用程序的上下文中执行此操作吗?当我尝试这样做时,我必须在我的应用程序的 Activity 中吗?需要围绕这个重新设计会有点烦人,能够发送异常电子邮件会非常有用!

编辑:Rich 提出了一个很好的问题,这就是为什么我首先要这样做的原因?答案很简单,应用程序中加载的数据可能会失败,我希望用户能够在发生这种情况时发送一些失败数据。

这是在应用程序而不是 Activity 中的原因是我不想在每次有人旋转设备时丢失应用程序状态变量。当我发现旋转时会调用 onPause 和 onCreate 这一事实(对于 Activies),我将这段代码重构到应用程序中(效果很好!)。我实际上已经尝试过将这些发送到 onConfigurationChanged 并在我的 Activity 中覆盖该方法的解决方案,但这很困惑,无论出于何种原因,尽管将 android:configChanges="keyboardHidden|orientation" 放在我的 list 中它不是'不适合我。我确定我可以选择这个选项,但我宁愿让事情保持原样(这样更干净)并且让某人能够发送电子邮件!

最佳答案

啊!我想出了我做了什么,这很简单!我在错误的 Intent 上设置了 FLAG_ACTIVITY_NEW_TASK!愚蠢的我,我错过了 Intent.createChooser(...,...) 将返回一个新的 Intent,因此您必须在 chooser Intent 而不是 ACTION_SEND Intent 上设置标志。

当你想到它时并没有那么困惑,我不敢相信我忽略了这一点!

所以如果有人做了我做过的事,给你:

public void sendNotificationEmail(String emailBody) {
        Intent emailIntent = new Intent(Intent.ACTION_SEND);
        emailIntent.setType("text/html");
        emailIntent.putExtra(Intent.EXTRA_EMAIL, notificationRecipients);
        emailIntent.putExtra(Intent.EXTRA_SUBJECT, "MyAppName Error");
        emailIntent.putExtra(Intent.EXTRA_TEXT, emailBody);
        Intent emailChooser = Intent.createChooser(emailIntent, "An error has occurred! Send an error report?");
        emailChooser.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        try {
            startActivity(emailChooser);
        } catch (ActivityNotFoundException e) {
            // If there is nothing that can send a text/html MIME type
            e.printStackTrace();
        }
    }

关于android-activity - Application 子类中的 startActivity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7746274/

相关文章:

java - 更改 Activity 会阻止 ImageButton setImageResource 工作

java - 检查 Android 中另一个线程中的任务是否已经完成

java - 我应该设置时区将字符串转换为日期吗?

android - 以编程方式更改图层列表中形状的颜色

android - 如果已经登录,请正确跳过登录 Activity

java - 完成 Activity 后修剪回历史

android - 调用 finish() 不会停止代码的执行

java - 如何为 spotify sdk android 制作队列

android - View.LAYER_TYPE_HARDWARE 使 NestedScrollView 内的 WebView 崩溃

android - 在 android 中播放 mp3 文件时遇到问题