Skip to content

Commit 3d46794

Browse files
Fred ChungAndroid (Google) Code Review
authored andcommitted
Merge "Updated "Making your App Location Aware" class to include information on location provider enable check." into ics-mr1
2 parents 38e09c3 + 9340e21 commit 3d46794

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed
220 KB
Binary file not shown.

docs/html/training/location/locationmanager.jd

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ next.link=currentlocation.html
1818
<li><a href="locationmanager.html#TaskDeclarePermissions">Declare Proper Permissions in Android Manifest</a></li>
1919
<li><a href="locationmanager.html#TaskGetLocationManagerRef">Get a Reference to LocationManager</a></li>
2020
<li><a href="locationmanager.html#TaskPickLocationProvider">Pick a Location Provider</a></li>
21+
<li><a href="locationmanager.html#TaskVerifyProvider">Verify the Location Provider is Enabled</a></li>
2122
</ol>
2223

2324
<h2>You should also read</h2>
@@ -88,3 +89,32 @@ if (providerName != null) {
8889
...
8990
}
9091
</pre>
92+
93+
<h2 id="TaskVerifyProvider">Verify the Location Provider is Enabled</h2>
94+
95+
<p>Some location providers such as the GPS can be disabled in Settings. It is good practice to check whether the desired location provider is currently enabled by calling the {@link android.location.LocationManager#isProviderEnabled(java.lang.String) isProviderEnabled()} method. If the location provider is disabled, you can offer the user an opportunity to enable it in Settings by firing an {@link android.content.Intent} with the {@link android.provider.Settings#ACTION_LOCATION_SOURCE_SETTINGS} action.</p>
96+
97+
<pre>
98+
&#64;Override
99+
protected void onStart() {
100+
super.onStart();
101+
102+
// This verification should be done during onStart() because the system calls
103+
// this method when the user returns to the activity, which ensures the desired
104+
// location provider is enabled each time the activity resumes from the stopped state.
105+
LocationManager locationManager =
106+
(LocationManager) getSystemService(Context.LOCATION_SERVICE);
107+
final boolean gpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
108+
109+
if (!gpsEnabled) {
110+
// Build an alert dialog here that requests that the user enable
111+
// the location services, then when the user clicks the "OK" button,
112+
// call enableLocationSettings()
113+
}
114+
}
115+
116+
private void enableLocationSettings() {
117+
Intent settingsIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
118+
startActivity(settingsIntent);
119+
}
120+
</pre>

0 commit comments

Comments
 (0)