1 00:00:00,320 --> 00:00:02,091 Enough of this stopping services stuff. 2 00:00:02,091 --> 00:00:06,010 Let's get back to the par with the invincible service refuses today. 3 00:00:08,321 --> 00:00:10,650 But first a quick recap. 4 00:00:10,650 --> 00:00:14,890 The return value of onStarCommand represents what will happen to a service 5 00:00:14,890 --> 00:00:19,150 if its process is killed after returning from onStarCommand, 6 00:00:19,150 --> 00:00:20,450 but before being stopped. 7 00:00:21,660 --> 00:00:22,870 Put more simply, 8 00:00:22,870 --> 00:00:26,550 it's what will happen if your service is killed without finishing its work. 9 00:00:28,220 --> 00:00:31,495 Earlier I mentioned that there are three constants we can return from 10 00:00:31,495 --> 00:00:32,930 onStarCommand. 11 00:00:32,930 --> 00:00:37,707 Those constants are START_STICKY, START_NOT_STICKY, and 12 00:00:37,707 --> 00:00:40,000 START_REDELIVER_INTENT. 13 00:00:40,000 --> 00:00:44,150 There's actually a fourth value START_STICKY_COMPATIBILITY. 14 00:00:44,150 --> 00:00:48,216 But it's not important to learn about, so we've just got START_STICKY, 15 00:00:48,216 --> 00:00:51,340 START_NOT_STICKY, and START_REDELIVER_INTENT. 16 00:00:52,520 --> 00:00:55,070 Let's kick things off with START_STICKY. 17 00:00:55,070 --> 00:00:57,501 Returning Service.START_STICKY and 18 00:00:57,501 --> 00:01:02,010 onStartCommand tells Android to restart our service using a null intent. 19 00:01:03,050 --> 00:01:07,520 This is useful for things you might want to leave running for a long time. 20 00:01:07,520 --> 00:01:11,730 Like a music player or an app to track where you went on a run. 21 00:01:11,730 --> 00:01:16,350 Once these things are started, it's up to the user to decide when to stop them. 22 00:01:17,380 --> 00:01:22,010 If I'm out running and Android kills my run tracking app, I'm going to be 23 00:01:22,010 --> 00:01:25,390 pretty unhappy if it doesn't come back and track the rest of my run. 24 00:01:25,390 --> 00:01:29,850 S is just what you think it would be. 25 00:01:29,850 --> 00:01:34,020 If we return Service.START_NOT_STICKY from onStartCommand, 26 00:01:34,020 --> 00:01:36,540 then our service won't be restarted. 27 00:01:36,540 --> 00:01:40,220 An example of this would be a service that checks for new emails in your inbox. 28 00:01:41,300 --> 00:01:43,880 And let say this service is started every five minutes. 29 00:01:44,970 --> 00:01:46,550 So every five minutes, 30 00:01:46,550 --> 00:01:51,550 this service will run and update your inbox with any new messages. 31 00:01:51,550 --> 00:01:55,050 But if Android is so burdened that it kills your app, 32 00:01:55,050 --> 00:01:59,920 it's probably not the best idea to throw rescheduling this service into the mix. 33 00:02:00,960 --> 00:02:04,130 After all, it'll be started again in five minutes anyway. 34 00:02:06,110 --> 00:02:09,590 Last, but not least, is START_REDELIVER_INTENT. 35 00:02:09,590 --> 00:02:12,720 If we return Service.START_REDELIVER_INTENT 36 00:02:12,720 --> 00:02:17,330 from onStartCommand, our service will be restarted using the last delivered intent. 37 00:02:18,510 --> 00:02:20,540 In fact, for every intent, 38 00:02:20,540 --> 00:02:25,500 if the associated start ID hasn't been included in a call to stop self 39 00:02:25,500 --> 00:02:29,060 then our service's onStartCommand method will be called with the intent. 40 00:02:30,460 --> 00:02:32,080 Here's an example. 41 00:02:32,080 --> 00:02:35,880 Let's say we've downloaded the first two songs and then our process gets killed. 42 00:02:35,880 --> 00:02:40,137 Since we've only downloaded two songs we haven't yet 43 00:02:40,137 --> 00:02:43,290 called stopSelf for songs 3, 4 or 5. 44 00:02:43,290 --> 00:02:48,110 So the three intense associated with these songs will be redelivered to 45 00:02:48,110 --> 00:02:50,530 our service when it's restarted. 46 00:02:51,980 --> 00:02:53,230 And there we go. 47 00:02:53,230 --> 00:02:56,940 Those are the three things we can have happen when our process is killed 48 00:02:56,940 --> 00:02:59,280 while our service is still working. 49 00:02:59,280 --> 00:03:03,900 We can restart the service with a null intent, START_STICKY, 50 00:03:03,900 --> 00:03:08,960 not restart the service, START_NOT_STICKY, or restart the service and 51 00:03:08,960 --> 00:03:12,895 redeliver all unfinished intents, START_REDELIVER_INTENT.