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

Debugger
  • 2,123
  • 1
  • 17
  • 39
  • possible duplicate of [How to start an Activity from a Service?](http://stackoverflow.com/questions/3456034/how-to-start-an-activity-from-a-service) – Damian Jeżewski Jan 18 '15 at 11:39
  • Calling `startActivity()` from a `Service` is a significant code smell. Why are you doing this? – CommonsWare Jan 18 '15 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.\ – Debugger Jan 18 '15 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) – Debugger Jan 18 '15 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. – Damian Jeżewski Jan 18 '15 at 15:33

1 Answers1

0

Below is my code

Call addFlags() before startActivity().

So on the service class there is a layout

That is atypical for a service.

CommonsWare
  • 910,778
  • 176
  • 2,215
  • 2,253