应用程序的子类中startActivity应用程序、类中、startActivity

2023-09-12 04:40:14 作者:再辛苦也不说

我在其中我直接指定我的应用程序,并做一些应用程序范围设定在ApplicationSubclass的onCreate一点点的Andr​​oid应用程序,但我得到以下错误(请注意,我知道FLAG_ACTIVITY_NEW_TASK):

I have a little Android application in which I specify my application directly and do some application-wide setup in the ApplicationSubclass' onCreate, but I am getting the following error (Note, I know about 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当一些抛出异常,我抓住他们,使应用程序的用户可以在一个小电子邮件发送的信息的例外,这样我就可以更轻松地解决可能出现的任何东西或指导他们固定的问题。

I am calling this sendNotificationEmail when some exceptions are thrown and I catch them, so that the user of the app can send in a little e-mail with information on the exception so that I can more easily fix anything that might arise or guide them on fixing their issue.

下面是一些相关的code:

Here is some of the relevant code:

manifest.xml的:

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>

的ApplicationSubclass定义为:

the ApplicationSubclass is defined as:

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();
      }
   }
}

我想知道为什么会这样。我看了一些资料,我找的计算器一些答案,并在互联网上大。在我看来,我应该没事,因为我设置FLAG_ACTIVITY_NEW_TASK,但是这显然不是这样的!

I'd like to know why this is happening. I read some documentation, I looked for some answers on StackOverflow and the Internet at large. It seems to me that I should be fine since I am setting FLAG_ACTIVITY_NEW_TASK, however that's clearly not the case!

难道仅仅是我可能甚至没有试图从应用程序的情况下做到这一点?我必须在我的应用程序中的活动中,当我尝试呢?这将是那种恼人的需要重新设计解决这个问题,并能够发送异常的电子邮件将是非常有用的!

Is it simply that I may not even attempt to do this from the Application's context? Must I be within a Activity in my application when I attempt this? It would be sort of annoying to need to re-engineer around this, and being able to send exception e-mails would be quite useful!

修改:富问了一个很好的问题,这就是为什么赫克我会想首先要这么做?答案很简单,就是有被加载可能失败的应用数据,我希望用户能够发送一些故障数据时发生这种情况。

EDIT: Rich asked a good question, and that's why the heck would I want to be doing this in the first place? The answer is simply that there is data being loaded in the Application that could fail, and I'd like the user to be able to send some of that failure data when this occurs.

究其原因,这是在应用程序,而不是活动是有应用程序的状态变量,我不想失去每次有人旋转设备。当我发现了一个事实,即在onPause和的onCreate当你旋转(适用于Activies)被称为我重构这个code伸到应用(伟大的工程!)。我实际上试图解决我的活动把这些给onConfigurationChanged和覆盖的方法,但这是凌乱而无论出于何种原因,尽管把安卓configChanges =keyboardHidden |定位在我的表现也没有正确为我工作。我敢肯定,我的可以的追求这个选项,但我宁愿离开的事情,因为它们是(它的清洁剂)的和的获取能力的人到电子邮件!

The reason this is in the Application and not the activity is that there are application state variables I don't want to lose every time someone rotates the device. When I found out about the fact that onPause and onCreate are called when you rotate (for Activies) I refactored this code out into the Application (which works great!). I had actually tried the solution to send these to onConfigurationChanged and Override that method in my activity, but that's messy and for whatever reason despite putting android:configChanges="keyboardHidden|orientation" in my manifest it wasn't correctly working for me. I'm sure I could pursue this option, but I'd rather leave things as they are (it's cleaner) and get the ability for someone to e-mail!

推荐答案

啊!我想通了,我做什么,这是很简单的!我设置了FLAG_ACTIVITY_NEW_TASK在错误的意图!我傻,我错过了Intent.createChooser(...,...)将返回一个新的意图,所以你必须设置标志上的选择器的意图,而不是在ACTION_SEND意向。

Ah! I figured out what I did, it's quite simple! I was setting the FLAG_ACTIVITY_NEW_TASK on the wrong intent! Silly me, I missed that Intent.createChooser(...,...) will return a new Intent, so you must set the flag on the chooser Intent rather than on the ACTION_SEND Intent.

而不是当你想想看,我不能相信我忽视的是,所有的混乱!

Not all that confusing when you think about it, and I can't believe I overlooked that!

因此​​,如果有谁做什么我做了,在这里你去:

So if anyone ever does what I did, here you go:

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();
        }
    }
 
精彩推荐
图片推荐