Application子类中的startActivity

Kev*_*vek 6 android android-activity

我有一个小的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)
Run Code Online (Sandbox Code Playgroud)

当我抛出一些异常时,我正在调用这个sendNotificationEmail,我抓住它们,以便应用程序的用户可以发送一个包含异常信息的小电子邮件,以便我可以更轻松地修复可能出现的任何内容或引导它们解决他们的问题.

以下是一些相关代码:

manifest.xml文件:

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

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();
      }
   }
}
Run Code Online (Sandbox Code Playgroud)

我想知道为什么会这样.我阅读了一些文档,我在StackOverflow和整个Internet上寻找了一些答案.在我看来,我应该没事,因为我设置FLAG_ACTIVITY_NEW_TASK,但事实显然不是这样!

是否只是我甚至不尝试从应用程序的上下文中执行此操作?当我尝试这个时,我必须在我的应用程序的活动中吗?需要对此进行重新设计会很烦人,能够发送异常电子邮件会非常有用!

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

这是在应用程序而不是活动中的原因是,每当有人旋转设备时,我都不希望丢失应用程序状态变量.当我发现旋转时调用onPause和onCreate这一事实时(对于Activies)我将这个代码重构为Application(这很好用!).我实际上已经尝试过将这些方法发送到onConfigurationChanged并在我的活动中覆盖该方法,但是这很麻烦,无论出于什么原因,尽管放入android:configChanges="keyboardHidden|orientation"我的清单它并没有正确地为我工作.我确信我可以选择这个选项,但我宁愿保留原样(它更干净)并且能够让某人发送电子邮件!

Kev*_*vek 17

啊! 我弄清楚我做了什么,这很简单!我错误地设置了FLAG_ACTIVITY_NEW_TASK!傻傻的我,我错过了Intent.createChooser(...,...)将返回一个新的Intent,所以你必须在选择器 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();
        }
    }
Run Code Online (Sandbox Code Playgroud)


Day*_*man 6

我从我的类中启动了一个活动(在丢失会话时重新登录用户),该类是Application的子类,如下所示:

public boolean relogin(Activity act) {      
    Intent intent = new Intent(act,ActivityLogin.class);    
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);     
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    startActivity(intent);              
    act.finish();// here i finish my current activity
}
Run Code Online (Sandbox Code Playgroud)

  • 这不应该是setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP)? (2认同)