1
00:00:00,180 --> 00:00:05,070
Hire everyone in this story I want to add another functionality to this application.

2
00:00:05,400 --> 00:00:13,370
So this functionality is actually going to allow the users to be notified whenever they use it actually

3
00:00:13,380 --> 00:00:16,160
changes his or her location.

4
00:00:16,230 --> 00:00:19,620
Actually the physical location of the Android device.

5
00:00:19,680 --> 00:00:20,220
Right.

6
00:00:20,520 --> 00:00:24,870
So the user does not have to click on the button.

7
00:00:24,870 --> 00:00:27,700
Get that data button in order to actually get there.

8
00:00:27,720 --> 00:00:33,470
For example the time that is remains to get to the destination location or the distance that is actually

9
00:00:33,470 --> 00:00:36,050
made through this to get to the destination location.

10
00:00:36,470 --> 00:00:42,590
I want the app to automatically send this data to the user.

11
00:00:42,770 --> 00:00:45,810
They use it to be notified automatically about these data up.

12
00:00:45,810 --> 00:00:49,770
You are going to actually update our text views automatically.

13
00:00:49,770 --> 00:00:51,800
So how we can do that.

14
00:00:51,810 --> 00:00:53,350
I want to show you that here.

15
00:00:53,490 --> 00:00:57,810
First of all we need to add some more codes here inside our main activity class.

16
00:00:57,840 --> 00:00:59,480
So lets do that now.

17
00:00:59,670 --> 00:01:03,170
First of all we need to go into the unconnected method.

18
00:01:03,330 --> 00:01:07,080
So let's actually find the Smurfette let's search for this method here.

19
00:01:07,080 --> 00:01:12,060
So if you are using a Mac you just need to hold down the command on your keyboard and then press and

20
00:01:12,060 --> 00:01:16,650
then underneath that if you and your keyboard and if you are using a Windows machine you just need to

21
00:01:16,650 --> 00:01:20,800
hold down the control on your keyboard and then press and delete that if on your keyboard.

22
00:01:20,940 --> 00:01:25,950
So let's search for the unconnected method right.

23
00:01:27,030 --> 00:01:33,390
So here it is are unconnected my faith and now here we are just actually outputting some values to the

24
00:01:33,480 --> 00:01:34,160
log.

25
00:01:34,410 --> 00:01:39,720
So now after this line of code as you can see I actually commented out this method here.

26
00:01:39,720 --> 00:01:40,020
Right.

27
00:01:40,020 --> 00:01:41,890
We are not going to use this method anymore.

28
00:01:42,010 --> 00:01:45,500
Here it is and if it is used in order to get the users current location.

29
00:01:45,780 --> 00:01:51,980
So after this line of code I am going to get the diffuse location provide that API.

30
00:01:52,170 --> 00:01:59,070
So first this actually refers to this fuse location provide that API interface and I need to actually

31
00:01:59,070 --> 00:02:05,130
give a name to this variable faced effused location provide that API assignment operator because this

32
00:02:05,130 --> 00:02:12,760
is actually an interface we can not use the new keyboard in order to create an instance of a fuse location.

33
00:02:12,860 --> 00:02:13,840
Provider API.

34
00:02:14,190 --> 00:02:17,900
So here I'm going to refer to the location services here.

35
00:02:17,910 --> 00:02:24,240
Location Services dot dot fuse location provide the API semicolon.

36
00:02:24,380 --> 00:02:29,740
So in this way we are initializing our variable which is of type fuse location pro-white API.

37
00:02:29,810 --> 00:02:34,810
Right now I can't say because we want to actually get the users.

38
00:02:34,910 --> 00:02:41,480
Actually I want to get to the destination that it's remained to the destination to the destination location

39
00:02:41,510 --> 00:02:47,360
or the time that is left to get to the destination location automatically Vinney to actually create

40
00:02:47,510 --> 00:02:52,300
a loop location request object in order to do that job automatically for us.

41
00:02:52,490 --> 00:02:59,050
So after this line of code here in line number 161 I am going to say location request.

42
00:02:59,090 --> 00:03:02,630
So this is the type of the object that I am going to create right.

43
00:03:02,690 --> 00:03:06,760
So the name of a variable is going to be location request assignment operator.

44
00:03:06,790 --> 00:03:11,230
You can see a new location request semicolon.

45
00:03:11,270 --> 00:03:18,560
So now we have an object of application requests that can request the location so that we can actually

46
00:03:18,560 --> 00:03:24,470
specify the time that is actually left to get to the destination location or the distance that is left

47
00:03:24,470 --> 00:03:25,950
to get to the destination location.

48
00:03:25,970 --> 00:03:29,510
So we must actually create a location or request for that.

49
00:03:29,510 --> 00:03:36,140
So after this line of code I can't say so first of all we need to specify the interval of the location

50
00:03:36,140 --> 00:03:44,500
request so I can say location request that set interval and here I can say ten thousand right as you

51
00:03:44,500 --> 00:03:46,450
can see it is of the milliseconds right.

52
00:03:46,450 --> 00:03:47,140
What does this mean.

53
00:03:47,140 --> 00:03:50,270
This means that 10000 means 10 seconds.

54
00:03:50,440 --> 00:03:58,470
So after 10 seconds it the location eques is going to be he's going to actually request the location.

55
00:03:58,480 --> 00:04:05,480
So this means that every 10 seconds the location is going to be a request that right.

56
00:04:05,490 --> 00:04:07,650
If you want to you can actually change this value if you want.

57
00:04:07,650 --> 00:04:13,080
For example after 50 seconds we can change the value if you want.

58
00:04:13,080 --> 00:04:17,520
For example after five seconds you can change the value to 5000.

59
00:04:17,700 --> 00:04:22,520
But I want that after this request to be executed after 10 seconds.

60
00:04:22,530 --> 00:04:23,070
OK.

61
00:04:23,190 --> 00:04:30,050
So after this line of code I can't say location request that sets priority.

62
00:04:30,120 --> 00:04:32,870
So let's see what values we can use here.

63
00:04:32,880 --> 00:04:38,550
So here I can't say for example just they've been here priority right.

64
00:04:38,770 --> 00:04:40,460
And now look at these values here.

65
00:04:40,500 --> 00:04:44,570
Let me just add to the whole vault here.

66
00:04:44,840 --> 00:04:48,730
So you can see that the first one is priority balanced power.

67
00:04:48,740 --> 00:04:50,240
Look at this one here.

68
00:04:50,270 --> 00:04:55,960
It is used it is it is not going to actually use its battery a lot.

69
00:04:55,970 --> 00:05:01,600
OK it is just going to actually for example just it's the use of a battery a little bit so that it is

70
00:05:01,700 --> 00:05:04,330
its battery is not going to be drained.

71
00:05:04,820 --> 00:05:11,610
And the I mean by you by saying use of Especially I mean the users the device back to you OK.

72
00:05:11,900 --> 00:05:16,230
So their first option is this one priority balance power.

73
00:05:16,310 --> 00:05:22,930
So if you choose this one the user's device battery is not going to be drained dramatically.

74
00:05:23,010 --> 00:05:28,850
It is just it's just going to actually it a little bit of the battery power so that it's going to get

75
00:05:28,850 --> 00:05:31,220
the location of the user.

76
00:05:31,460 --> 00:05:38,140
And the second option is high accuracy this option is used if you want to get the highest accuracy location.

77
00:05:38,150 --> 00:05:42,730
So if you want to use this option this option is going to actually eat a lot of battery.

78
00:05:42,770 --> 00:05:43,680
OK.

79
00:05:44,180 --> 00:05:45,970
The first option is low power.

80
00:05:45,980 --> 00:05:54,110
So it is going to actually just eat a lot do a little bit actually of the user's device past the battery.

81
00:05:54,110 --> 00:05:55,820
And the final option is this one.

82
00:05:55,820 --> 00:05:56,860
No power.

83
00:05:56,990 --> 00:06:00,410
So it is going to give you the worst location possible.

84
00:06:00,410 --> 00:06:07,850
It is not going to give you a very high accurate location because it is not going to eat a lot of battery

85
00:06:07,850 --> 00:06:08,940
power.

86
00:06:08,960 --> 00:06:15,170
So for this application if you're going to use actually to balance out power so that the first value

87
00:06:15,170 --> 00:06:21,480
is that the default the default value is actually this one balance priority balanced power if you want

88
00:06:21,480 --> 00:06:22,910
to you can choose this one.

89
00:06:23,000 --> 00:06:24,800
But the default value is this one.

90
00:06:24,860 --> 00:06:28,780
You don't have to explicitly specify that's why you're here.

91
00:06:28,860 --> 00:06:31,010
Right so I'm just going to put it here for you.

92
00:06:31,400 --> 00:06:38,860
And the first line of code that I am going to say here is that location requires thought displacements

93
00:06:38,870 --> 00:06:39,990
to just type in here.

94
00:06:40,300 --> 00:06:43,510
It set smallest displacement or kit.

95
00:06:43,520 --> 00:06:48,380
We have a getter and the center which as this one said smallest displacement.

96
00:06:48,470 --> 00:06:50,380
And here I am going to assign the value 5 here.

97
00:06:50,390 --> 00:06:51,230
So what is this.

98
00:06:51,230 --> 00:06:55,210
What is this smallest displacement here.

99
00:06:55,640 --> 00:07:06,470
After each for example after 5 meters for example the user has changed its device's location and after

100
00:07:06,470 --> 00:07:14,090
five meters I want to execute it for example get the request I want to actually send a request in order

101
00:07:14,090 --> 00:07:17,810
to get the location of the user's device.

102
00:07:17,810 --> 00:07:23,990
So every five meters the request is going to be actually executed.

103
00:07:24,500 --> 00:07:29,180
So this is disvalue is that if you want to you can for example change the value to 200.

104
00:07:29,180 --> 00:07:35,970
For example after each 200 200 meters the location is going to be requested.

105
00:07:36,380 --> 00:07:38,120
So this is for that.

106
00:07:38,120 --> 00:07:46,450
And now I can say if Google API client will if a claim that is connected.

107
00:07:46,830 --> 00:07:48,560
If it is actually connected actually.

108
00:07:48,570 --> 00:07:55,600
So we created an if statement and he said the curly braces of this statement I can't say fuse location

109
00:07:55,600 --> 00:08:02,260
provide that API that request location location updates to this method.

110
00:08:02,650 --> 00:08:07,140
And now as you can see we need to provide free arguments for this method.

111
00:08:07,150 --> 00:08:13,060
The first one is the Google client so we can actually say Google if a client we have available for that.

112
00:08:13,060 --> 00:08:18,670
The second argument is the location requests so we already created a location request here which is

113
00:08:18,850 --> 00:08:19,450
prime.

114
00:08:19,470 --> 00:08:25,580
Yet with these arguments right for example the entire file ten thousand the priority priority balance

115
00:08:25,580 --> 00:08:29,590
the power accuracy and the smallest displacement displacement five meters.

116
00:08:29,590 --> 00:08:30,110
Right.

117
00:08:30,340 --> 00:08:36,350
So we need to pass the location request here and now we need to specify the first argument.

118
00:08:36,350 --> 00:08:41,630
So let me actually right this minute they get in in order to show you the argument here again.

119
00:08:41,650 --> 00:08:44,330
So the first one is Google a client.

120
00:08:44,560 --> 00:08:50,250
The second one is location request and the first one you can see is the location listener.

121
00:08:50,320 --> 00:08:55,620
So what is this Vieni to provide a location listener here right.

122
00:08:55,630 --> 00:09:04,280
Otherwise you are not going to be actually allowed to notify the application about the location changes.

123
00:09:04,360 --> 00:09:06,270
So lets do that now.

124
00:09:06,370 --> 00:09:11,350
So now you may ask that so what is going to be the listener the listener is going to be the main activity

125
00:09:11,350 --> 00:09:18,200
class the main activity class is going to be the listener to the changes of the actually places of the

126
00:09:18,210 --> 00:09:19,770
users device.

127
00:09:19,930 --> 00:09:21,450
OK but here we have an error.

128
00:09:21,460 --> 00:09:28,570
If I hover over this area it says cannot resolve Mefford request location updates and you can see that

129
00:09:28,570 --> 00:09:35,950
it says that if we passed an invalid context here we passed the main activity that this is main activity

130
00:09:35,950 --> 00:09:41,570
here is not listening to the changes of the users actually the device location.

131
00:09:41,590 --> 00:09:49,150
So really to allow this class main activity to listen to the location changes how we can do that we

132
00:09:49,200 --> 00:09:56,440
need to actually say that this main If the class is conforming or implementing the location listener

133
00:09:56,470 --> 00:09:57,280
interface.

134
00:09:57,310 --> 00:10:00,740
So before that we need to actually scroll to the top.

135
00:10:01,070 --> 00:10:06,190
And now as you can see we are we are already implementing some interfaces here.

136
00:10:06,400 --> 00:10:11,590
And the final interface here is our view that uncock us and lets put a comma here and now we need to

137
00:10:11,590 --> 00:10:17,410
also actually conform to another interface meaning to implement another interface which is called location

138
00:10:17,800 --> 00:10:21,470
listener to this one and then present it to the keyboard.

139
00:10:21,520 --> 00:10:28,570
So now that we are actually conforming or implementing to the location listener interface we must implement

140
00:10:28,990 --> 00:10:32,610
the location change method that is inside this interface.

141
00:10:32,620 --> 00:10:39,380
So now let me actually scroll down here after the end in curly brace of this uncreate Smurfette here

142
00:10:40,490 --> 00:10:48,060
I can actually say on location changed right and press and President to change in order to select the

143
00:10:48,060 --> 00:10:48,680
Smurfette.

144
00:10:48,860 --> 00:10:55,370
So now we have this method right now we are a hundred and fifty percent sure that diskless main activity

145
00:10:55,580 --> 00:10:58,730
is actually listening to the last location changes.

146
00:10:58,730 --> 00:11:03,260
If I scroll down here lets actually find the uncorrect unconnected Meffert you can see that there is

147
00:11:03,260 --> 00:11:05,450
no error here right anymore.

148
00:11:05,930 --> 00:11:11,080
So now after this line of code here we can see that we are actually requesting the locations for us

149
00:11:11,090 --> 00:11:16,190
we are passing the Google IPO client and then we are putting the location request with these parameters

150
00:11:16,520 --> 00:11:21,340
and then we are saying that the listener is diskless main activity class this context right.

151
00:11:21,620 --> 00:11:26,540
So what happens if the application is not actually connected.

152
00:11:26,570 --> 00:11:29,480
In that case we are going to try to connect.

153
00:11:29,510 --> 00:11:35,930
So after the end in curly brace of this if statement I cant say else here I cant say Google for a client

154
00:11:36,290 --> 00:11:38,560
that can act semicolon.

155
00:11:38,840 --> 00:11:40,690
So if you are going to try again.

156
00:11:40,910 --> 00:11:42,230
Right.

157
00:11:42,230 --> 00:11:49,310
We need to be connected to the Google Play services in order to actually access the location changes.

158
00:11:49,370 --> 00:11:55,540
So now that we have this line of code now its time to go to the location change method here.

159
00:11:55,580 --> 00:11:59,120
So this is the method of the interface or location of the center.

160
00:11:59,120 --> 00:12:07,370
So inside this method I can easily say on collidge So here I am referring to dismiss that uncle when

161
00:12:07,370 --> 00:12:08,260
the button is pressed.

162
00:12:08,260 --> 00:12:08,840
Right.

163
00:12:08,910 --> 00:12:12,450
This Let me show you the design activity and then I mean that example.

164
00:12:12,590 --> 00:12:16,990
So when this button is pressed the Anglican faith is going to be actually executed.

165
00:12:17,300 --> 00:12:24,680
So because you want to get the location changes automatically without forcing the user to press on the

166
00:12:24,680 --> 00:12:33,220
spot in I want the app to be able to get the location changes automatically without pressing this button

167
00:12:33,220 --> 00:12:39,160
so let's go to them in the class and here I can say inside is on location change it.

168
00:12:39,360 --> 00:12:42,140
I can say that this is going to be executed.

169
00:12:42,160 --> 00:12:43,110
Unclick.

170
00:12:43,360 --> 00:12:45,050
So I just refer to dismiss it Anklage.

171
00:12:45,190 --> 00:12:48,370
And now you can see that if it needs an argument of type view.

172
00:12:48,520 --> 00:12:54,600
So here because we don't have the view and we are 100 percent 150 percent sure that we have only one

173
00:12:54,610 --> 00:13:00,910
button inside our view we are just going to pass on the knowledge here as an argument because we are

174
00:13:00,910 --> 00:13:06,490
sure that this effort is going to be executed only when the user presses on the spot and get the data.

175
00:13:06,490 --> 00:13:07,270
So let's go to that.

176
00:13:07,270 --> 00:13:09,280
Let's go back to the and activity class.

177
00:13:09,340 --> 00:13:13,800
So what happens when the app goes into background.

178
00:13:13,810 --> 00:13:15,790
So let let me show the emulator here.

179
00:13:15,790 --> 00:13:17,140
This is an application right.

180
00:13:17,620 --> 00:13:20,720
And then I actually press on this.

181
00:13:20,900 --> 00:13:23,590
I can hear the home I can hear if I click on it here.

182
00:13:23,610 --> 00:13:24,720
Now you can see that.

183
00:13:24,760 --> 00:13:27,370
Now the app goes into the background.

184
00:13:27,520 --> 00:13:31,480
In this case in this situation all you want.

185
00:13:31,520 --> 00:13:36,780
They're actually requesting the process of requesting the location to stop.

186
00:13:37,020 --> 00:13:37,580
OK.

187
00:13:37,660 --> 00:13:44,650
All I want only the app to request the locations when the app is actually on the foreground when the

188
00:13:44,650 --> 00:13:45,330
app is running.

189
00:13:45,370 --> 00:13:47,850
And the user is interacting with application.

190
00:13:47,910 --> 00:13:54,480
I don't want the app to actually drain the battery power when the app is in the background.

191
00:13:54,640 --> 00:13:55,220
Right.

192
00:13:55,300 --> 00:14:00,760
So that is a notice that when we are in the background the unpause method of the application is going

193
00:14:00,760 --> 00:14:01,600
to be caught.

194
00:14:01,630 --> 00:14:08,930
So the unposed method is the best place to stop at the location or getting to process.

195
00:14:08,950 --> 00:14:15,760
So now I can say after the curly brace of this on location changed method I can just step in and pause

196
00:14:16,430 --> 00:14:16,980
right.

197
00:14:17,230 --> 00:14:21,820
So after this line of code that actually caused the unpause on the superclass here I can't say.

198
00:14:21,880 --> 00:14:25,200
So first of all we need to actually get the fuse location provide that API.

199
00:14:25,210 --> 00:14:27,880
So just type in here fuse location provider API.

200
00:14:27,880 --> 00:14:34,230
Let's give it a name assignment operator and here I can say location services that fuse location provide

201
00:14:34,230 --> 00:14:35,690
that API semicolon.

202
00:14:35,710 --> 00:14:41,140
And now I can say use the location provider API that remove location updates.

203
00:14:41,140 --> 00:14:46,960
So we need to pass two arguments to the Smurfit the first one is Google API client come up and the second

204
00:14:47,140 --> 00:14:52,840
argument is actually the location listener the listener is diskless like the context main activity that

205
00:14:53,170 --> 00:14:54,990
this semicolon.

206
00:14:55,120 --> 00:14:56,610
That's it.

207
00:14:56,650 --> 00:15:04,060
Now again let me tell you that you must run this application on your actual device in order to see the

208
00:15:04,060 --> 00:15:08,560
results because the emulator does not have the feature to get the user's location.

209
00:15:08,560 --> 00:15:15,220
Or for example as specifying the distance or the time that is remains to that to get to the destination

210
00:15:15,220 --> 00:15:21,960
location because of the current location is going to be not the emulator is not able to get the look

211
00:15:22,000 --> 00:15:23,110
current location.

212
00:15:23,110 --> 00:15:30,180
So when you must actually test this app on your actual device and thats it this is the end of this application

213
00:15:30,490 --> 00:15:38,080
and you can actually extend this application and make it a very complex and full featured Android application

214
00:15:38,080 --> 00:15:40,310
and upload it to the Google Peyster and other.

215
00:15:40,360 --> 00:15:45,580
And I actually recommend you to do that because its a very powerful application.

216
00:15:45,580 --> 00:15:49,980
It can be of very great application for your resume.

217
00:15:49,990 --> 00:15:55,450
And if you want to get employed as an Android Developer its almost necessary for you to have Android

218
00:15:55,480 --> 00:15:57,290
apps in Google place.

219
00:15:57,640 --> 00:16:03,980
So I'm going to add another application to discourse about actually using the camera and taking picture.

220
00:16:04,300 --> 00:16:11,270
So in the next application which is going to be app seventy seven you are going to create that application

221
00:16:11,270 --> 00:16:16,330
in the next application is also going to be a production application so you can upload it right away

222
00:16:16,330 --> 00:16:17,300
to the Google Base there.

223
00:16:17,500 --> 00:16:19,400
So thank you very much for watching.

224
00:16:19,660 --> 00:16:24,610
If you have any questions about the concepts that I taught you in this lesson please post the discussion

225
00:16:24,610 --> 00:16:27,960
in the course and then I do my best to answer all of your questions.

