39

Is it possible to start an Activity from a Service? If yes, how can we achieve this?

SpunkerBaba
  • 2,097
  • 4
  • 18
  • 12

4 Answers4

72

android.app.Service is descendant of android.app.Context so you can use startActivity directly. However since you start this outside any activity you need to set FLAG_ACTIVITY_NEW_TASK flag on the intent.

For example:

Intent i = new Intent();
i.setClass(this, MyActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);

where this is your service.

class
  • 8,458
  • 25
  • 29
Marcin Gil
  • 62,506
  • 8
  • 57
  • 60
  • 1
    This is good but every time Activiyt will start like new started activity. And this kind of call is working for only once. Or I miss something!? – uzay95 Jun 20 '11 at 16:45
  • @MarcinGil do you know if this is intentional behavior? In other words do you know if the google engineers designed services to be able to open an activity without user interaction and if the process is always smooth? – MikeIsrael May 01 '12 at 06:44
  • @MikeIsrael I believe it is. Just imagine that system service that runs in the background is unable to display accept/reject activity on incoming call. Since you're the developer of the service and you know what activity you want to display it all depends on you. You can use services started from your app to perform some operations and eg. activate application on certain triggers; like update status in notification bar and start app on a click. – Marcin Gil May 01 '12 at 12:04
  • @MarcinGil that was more my question. Since there already is a mechanism (notifications) to request opening the application, would directly starting the activity be some kind of hack to remove the user input form the process of starting an application. – MikeIsrael May 01 '12 at 12:12
  • @MikeIsrael No hack. You can call `startActivity()` from Service just like in an answer. When do you do it -- it's up to you. As a quick example: implement a service that registers for a broadcast and then send this broadcast using alarms. In handling routine for broadcast start desired activity. This way you can start your service once, let it live after app is closed and open some activity upon certain trigger w/o further user interaction. – Marcin Gil May 08 '12 at 12:14
  • All of these is the same as how you would normally start an activity with one exception, the setFlag. Thanks for explaining this minor (very important) detail. Without the flag there will be an exception. – catalyst294 Aug 10 '14 at 00:38
  • Please help me to open application from background service. I have use FirebaseCloudMessaging. and I want to Display Custom Calling Screen on Mobile when I send calling notification from web So please help to open Application Activity. If I close app then only got notification but can not open app and its activity – Dipanki Jadav Oct 20 '16 at 09:19
  • I am using this piece of code to startActivity from service. It works fine regardless of application is open or close but when i call finish() method on click at OK button, then activity hides but app doesn't close , i mean it remains in the recent apps list and user can resume that again. How to fully close? as it is single activity and when am finishing it, it shouldn't let anything open. – Usman Rana Dec 22 '16 at 13:16
  • Don't know but I can start Activity directly from Service without any flag and it is working fine – Panchal Amit Nov 20 '17 at 07:19
21

Even if the framework allows you to start an Activity from a Service, it's probably not a proper solution. The reason is that the Service task may or may not be the focus of the user at the time the Service wishes to interact with the user. Interrupting what the user is currently doing is considered bad design form, especially from something that is supposed to be operating in the background.

Therefore, you should consider using a Notification with Notification Service, which carries a PendingIntent to launch the desired Activity when the user decides it is time to investigate. Think of it as delayed gratification.

MarkG
  • 441
  • 3
  • 10
  • preach man! I just hate it when my alarm interrupts my sleep by doing what i as the user told it to do... the point of allowing this is to allow the end user to make their own determinations as to what they consider important... if you're worried about interrupting what the user happens to be doing at the moment that they have requested a specific app to open, then you are not considering the needs and desires of the user. – me_ Jun 22 '18 at 18:31
  • 1
    answering phone calls comes to mind as a perfect example... you think that rather than being able to answer the call directly you should have to pull down the notification area, click the phone app notification then choose to answer, send a reason for not answering, or ignore a call? How is that more convenient for the user again? – me_ Jun 22 '18 at 18:40
7

I had a problem starting an activity from a service, it was due to the missing FLAG_ACTIVITY_NEW_TASK intent flag.

MongoDroid
  • 109
  • 1
  • 5
7

This surely will solve your problem

Intent intent = new Intent();
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ComponentName cn = new ComponentName(this, TaxiPlexer.class);
intent.setComponent(cn);
startActivity(intent);
Ram kiran
  • 20,129
  • 14
  • 55
  • 74
Rony
  • 347
  • 8
  • 17
  • Hello I have a Issue of start activity when app is close from Service (FCM) above code is work only when app is already open. Please help me for this to open app from background running service – Dipanki Jadav Oct 20 '16 at 09:59
  • @DipankiJadav Facing the same issue, did you find a way to fix this? – Aparna Dec 11 '19 at 13:14