0

I have a service class.I need to share my few details through Facebook,whatsapp,gmail,yahoo etc.But when i tried share intent i getting an error

android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?

Below is my code

shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain"); 
startActivity(Intent.createChooser(shareIntent,"Share via"));
shareIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Log.e("clicked","clicked>>>>>>share");

Please give me a solution

5
  • possible duplicate of How to start an Activity from a Service? Jan 18, 2015 at 11:39
  • Calling startActivity() from a Service is a significant code smell. Why are you doing this? Jan 18, 2015 at 12:37
  • @CommonsWare i have to share some test on gmail,Facebook etc.So on the service class there is a layout.When i click on the share button i need to show the list of apps.\ Jan 18, 2015 at 13:22
  • @DamianJeżewski: i dont want to start a new activity.I just want to show the share option like(share via Facebook,gmail,yahoo etc) Jan 18, 2015 at 13:37
  • @Debugger I exactly know what you'd like to achieve. The thing is that linked question contains answer for your question and also some additional info that explains what happens under the hood. Jan 18, 2015 at 15:33

1 Answer 1

0

Below is my code

Call addFlags() before startActivity().

So on the service class there is a layout

That is atypical for a service.

1
  • @Debugger: Then Android thinks that you are not calling addFlags() before startActivity(). Jan 18, 2015 at 13:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.