android - Why is the email Intent not working from Common (non Activity) class -


this code works in activity class, if move code common (non activity) class i'm getting error:

calling startactivity() outside of activity context requires flag_activity_new_task flag. want?

here code:

public static void emailintend(context context) {          intent emailintent = new intent(intent.action_sendto, null);          emailintent.putextra(intent.extra_subject, context.getstring(r.string.string_email_send_feedback_subject));         string[] receipients = new string[1];         receipients[0] = context.getstring(r.string.string_email);         emailintent.putextra(android.content.intent.extra_email, receipients);          emailintent.addflags(intent.flag_activity_new_task);          context.startactivity(intent.createchooser(emailintent, "send email developer..."));      } 

and how calling activity:

 common.emailintend( getapplicationcontext() ); 

i tried replacing getapplicationcontext() this, no help.

kindly, tell me if doing not correct.

the problem you're calling addflags(intent.flag_activity_new_task) on wrong intent.
that, combined using getapplicationcontext(), causing error.

the call intent.createchooser() returns new intent, , that's 1 needs flag_activity_new_task flag, since it's 1 you're passing startactivity().

note flag_activity_new_task wasn't needed if passed in activity context method (this in activity);

also note had modify code bit chooser work correctly, original code didn't work me, in activity.

the following code works me, using getapplicationcontext() context passed in:

public static void sendemail(context context){     string uritext =             "mailto:test@gmail.com" +                     "?subject=" + uri.encode("test subject");     uri uri = uri.parse(uritext);      intent emailintent = new intent(intent.action_sendto, uri);      intent = intent.createchooser(emailintent, "send email developer...");     i.addflags(intent.flag_activity_new_task);      context.startactivity(i); } 

references:

action_sendto sending email

startactivity within subclass of application


Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -