spazjr01
asked on
How do I stop my application from crashing when starting an android email intent? I had a Xamarin Crossplatform netstandard 1.6 project for Android and iOS.
How do I stop my application from crashing when starting an android email intent? I had a Xamarin Crossplatform netstandard 1.6 project for Android and iOS.
I developed a dependency service for sending an email from xamarin.forms. This used to work just fine. But, after a few Xamarin.Forms updates and related Xamarin.Android... API updates, this stopped working. At one point, I had to change the way I get the context to call the StartActivity(email) method.
Now, the email activity starts and I am able to send the email, but the app that called it crashes. And, I cannot figure out why. Is there something that I am doing wrong?
Method in common project (NetStandard 1.6)
private async Task EmailFileAsync(EmailConten t emailContent)
{
var emailer = DependencyService.Get();
Tuple<bool, string> response = new Tuple<bool, string>(false, "Unable to get an Email Manager from the device. ");
if (emailer != null)
{
response = await emailer.SendEmailAsync(ema ilContent) ;
}
if (!response.Item1)
{
await DisplayAlert("Send TRN Failure", response.Item2, "OK");
}
return response.Item1;
}
Dependency Service in Android project
using Android.App;
using Android.Content;
using System;
using System.IO;
using System.Threading.Tasks;
using TPS_Mobile_NS.Droid.Interf acesImplem ented;
using TPS_Mobile_NS.Interfaces;
using TPS_Mobile_NS.Models;
using Xamarin.Forms;
[assembly: Dependency(typeof(EmailMan ager))]
namespace TPS_Mobile_NS.Droid.Interf acesImplem ented
{
public class EmailManager : Activity, IEmailManager
{
public Task<Tuple<bool, string>> SendEmailAsync(EmailConten t emailContent)
{
var sendWasSuccessful = false;
var errorMessage = string.Empty;
try
{
var email = new Intent(Intent.ActionSend);
// Finish email.
email.PutExtra(Intent.Extr aSubject, emailContent.Subject);
email.PutExtra(Intent.Extr aText, emailContent.Body);
email.SetType("message/rfc 822");
email.AddFlags(ActivityFla gs.NewTask );
Context context = Android.App.Application.Co ntext;
context.StartActivity(emai l);
sendWasSuccessful = true;
return Task.FromResult(new Tuple<bool, string>(sendWasSuccessful, errorMessage));
}
catch(Exception ex)
{
errorMessage = ex.Message;
return Task.FromResult(new Tuple<bool, string>(sendWasSuccessful, errorMessage));
}
}
I developed a dependency service for sending an email from xamarin.forms. This used to work just fine. But, after a few Xamarin.Forms updates and related Xamarin.Android... API updates, this stopped working. At one point, I had to change the way I get the context to call the StartActivity(email) method.
Now, the email activity starts and I am able to send the email, but the app that called it crashes. And, I cannot figure out why. Is there something that I am doing wrong?
Method in common project (NetStandard 1.6)
private async Task EmailFileAsync(EmailConten
{
var emailer = DependencyService.Get();
Tuple<bool, string> response = new Tuple<bool, string>(false, "Unable to get an Email Manager from the device. ");
if (emailer != null)
{
response = await emailer.SendEmailAsync(ema
}
if (!response.Item1)
{
await DisplayAlert("Send TRN Failure", response.Item2, "OK");
}
return response.Item1;
}
Dependency Service in Android project
using Android.App;
using Android.Content;
using System;
using System.IO;
using System.Threading.Tasks;
using TPS_Mobile_NS.Droid.Interf
using TPS_Mobile_NS.Interfaces;
using TPS_Mobile_NS.Models;
using Xamarin.Forms;
[assembly: Dependency(typeof(EmailMan
namespace TPS_Mobile_NS.Droid.Interf
{
public class EmailManager : Activity, IEmailManager
{
public Task<Tuple<bool, string>> SendEmailAsync(EmailConten
{
var sendWasSuccessful = false;
var errorMessage = string.Empty;
try
{
var email = new Intent(Intent.ActionSend);
// Finish email.
email.PutExtra(Intent.Extr
email.PutExtra(Intent.Extr
email.SetType("message/rfc
email.AddFlags(ActivityFla
Context context = Android.App.Application.Co
context.StartActivity(emai
sendWasSuccessful = true;
return Task.FromResult(new Tuple<bool, string>(sendWasSuccessful,
}
catch(Exception ex)
{
errorMessage = ex.Message;
return Task.FromResult(new Tuple<bool, string>(sendWasSuccessful,
}
}
ASKER
I discovered further research that none of the posted code was the problem. The problem was caused earlier in the code from a poorly developed extended class. This class was used to create and store a file. In the process of doing that a null reference exception was created. However, it wasn't showing up as an error until the call to start the new Activity was made. So, it appeared as thought the StartActivity action was the cause of the problem. Once the class logic was fixed, there was no problem with the StartActivity call.
This question needs an answer!
Become an EE member today
7 DAY FREE TRIALMembers can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
ASKER
Maybe this additional information will help someone think of a solution:
// intent to start browser
string url = "http://www.google.com";
Intent browser = new Intent(Intent.ActionView, Android.Net.Uri.Parse(url)
Context context = Android.App.Application.Co
context.StartActivity(brow