0

I have an application that launches several Intents to send email, SMS, etc. In Android 4.0.4 and earlier all devices seem to work well, however in Android 4.1 and later, the application crashes, and no exception is being sent to logcat. I can reproduce this in the 4.1 emulator. Here is the code I am using to send email, for example.

Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.putExtra(Intent.EXTRA_SUBJECT, email.getSubject());
emailIntent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(email.getContent()));
emailIntent.putExtra(Intent.EXTRA_EMAIL, getRecipients(email.getTo()));
emailIntent.setType("text/html");
this.startActivityForResult(Intent.createChooser(emailIntent, "Choose Application"),1);
Peter O.
  • 32,158
  • 14
  • 82
  • 96
Casey
  • 184
  • 1
  • 3
  • 10
  • "the application crashes, and no exception is being sent to logcat" -- either the left or the right portion of this is incorrect. If the application crashes, by definition there is an exception in LogCat. If there is nothing in LogCat, by definition the application did not crash. Personally, I am stunned that this ever worked, as your `EXTRA_TEXT` is not `text/html`, because it is not text. Why are you converting HTML into a `CharSequence` via `Html.fromHtml()`? – CommonsWare Dec 06 '12 at 17:13
  • 2
    Can you add the LogCat error too? – Pavlos Dec 06 '12 at 17:13
  • Husam, The email object is just a pojo containing the data, nothing fancy there. – Casey Dec 06 '12 at 18:51
  • CommonsWare, you were correct in that the application is not really crashing, it's main activity is being reloaded, however there is no saved state information. I debugged and onSaveInstanceState is being called before the email client launches, but when you finish that activity, and the main activity resumes, there is no saved state causing the app to misbehave (restart). – Casey Dec 06 '12 at 19:26
  • What's really interested is that onCreate() get's called in Jellybean, but in ICS, it doesn't get called. Anyone have any idea what to do? – Casey Dec 06 '12 at 20:49

1 Answers1

0

This is due to the reason that you cannot do network operation from the any UI activity in newer versions of android. Folow this link to make it work.

private class LongOperation extends AsyncTask<String, Void, String> {

      @Override
      protected String doInBackground(String... params) {

Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.putExtra(Intent.EXTRA_SUBJECT, email.getSubject());
emailIntent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(email.getContent()));
emailIntent.putExtra(Intent.EXTRA_EMAIL, getRecipients(email.getTo()));
emailIntent.setType("text/html");
this.startActivityForResult(Intent.createChooser(emailIntent, "Choose Application"),1);

}

            return "Executed";
      }      

      @Override
      protected void onPostExecute(String result) {
                   }

      @Override
      protected void onPreExecute() {
      }

      @Override
      protected void onProgressUpdate(Void... values) {
      }
}   
Community
  • 1
  • 1
anurag
  • 82
  • 2
  • 10