Updated "Making your App Location Aware" class to include information on location provider enable check.
Change-Id: I97e0a6b02bccbfbdc5697953d2d8b63cb6089370
BIN
samples/training/location-aware/libs/android-support-v4.jar
Normal file
|
Before Width: | Height: | Size: 1.4 KiB After Width: | Height: | Size: 1.8 KiB |
|
Before Width: | Height: | Size: 1.4 KiB After Width: | Height: | Size: 1.7 KiB |
|
Before Width: | Height: | Size: 1.4 KiB After Width: | Height: | Size: 1.8 KiB |
|
After Width: | Height: | Size: 1.5 KiB |
|
Before Width: | Height: | Size: 3.5 KiB After Width: | Height: | Size: 1.4 KiB |
|
Before Width: | Height: | Size: 3.5 KiB After Width: | Height: | Size: 1.4 KiB |
|
Before Width: | Height: | Size: 3.3 KiB After Width: | Height: | Size: 1.4 KiB |
|
Before Width: | Height: | Size: 3.2 KiB After Width: | Height: | Size: 1.4 KiB |
|
Before Width: | Height: | Size: 3.3 KiB After Width: | Height: | Size: 1.4 KiB |
@@ -17,10 +17,12 @@
|
||||
<resources>
|
||||
<string name="app_name">Android Training: Location Update</string>
|
||||
<string name="address">Address:</string>
|
||||
<string name="enable_gps">Enable GPS</string>
|
||||
<string name="enable_gps_dialog">GPS provider not enabled. Would you like to enable it?</string>
|
||||
<string name="latlng">Lat/Long:</string>
|
||||
<string name="unknown">--Unknown--</string>
|
||||
<string name="not_support_gps">GPS provider not supported</string>
|
||||
<string name="not_support_network">Network provider not supported</string>
|
||||
<string name="use_fine_provider">Use fine provider</string>
|
||||
<string name="use_both_providers">Use both providers</string>
|
||||
<string name="use_fine_provider">Fine-grain provider</string>
|
||||
<string name="use_both_providers">Both providers</string>
|
||||
</resources>
|
||||
|
||||
@@ -17,8 +17,11 @@
|
||||
package com.example.android.location;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.app.Activity;
|
||||
import android.app.AlertDialog;
|
||||
import android.app.Dialog;
|
||||
import android.content.Context;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.Intent;
|
||||
import android.location.Address;
|
||||
import android.location.Geocoder;
|
||||
import android.location.Location;
|
||||
@@ -29,6 +32,9 @@ import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.os.Message;
|
||||
import android.provider.Settings;
|
||||
import android.support.v4.app.DialogFragment;
|
||||
import android.support.v4.app.FragmentActivity;
|
||||
import android.view.View;
|
||||
import android.widget.Button;
|
||||
import android.widget.TextView;
|
||||
@@ -38,7 +44,7 @@ import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
public class LocationActivity extends Activity {
|
||||
public class LocationActivity extends FragmentActivity {
|
||||
private TextView mLatLng;
|
||||
private TextView mAddress;
|
||||
private Button mFineProviderButton;
|
||||
@@ -122,6 +128,32 @@ public class LocationActivity extends Activity {
|
||||
setup();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onStart() {
|
||||
super.onStart();
|
||||
|
||||
// Check if the GPS setting is currently enabled on the device.
|
||||
// This verification should be done during onStart() because the system calls this method
|
||||
// when the user returns to the activity, which ensures the desired location provider is
|
||||
// enabled each time the activity resumes from the stopped state.
|
||||
LocationManager locationManager =
|
||||
(LocationManager) getSystemService(Context.LOCATION_SERVICE);
|
||||
final boolean gpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
|
||||
|
||||
if (!gpsEnabled) {
|
||||
// Build an alert dialog here that requests that the user enable
|
||||
// the location services, then when the user clicks the "OK" button,
|
||||
// call enableLocationSettings()
|
||||
new EnableGpsDialogFragment().show(getSupportFragmentManager(), "enableGpsDialog");
|
||||
}
|
||||
}
|
||||
|
||||
// Method to launch Settings
|
||||
private void enableLocationSettings() {
|
||||
Intent settingsIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
|
||||
startActivity(settingsIntent);
|
||||
}
|
||||
|
||||
// Stop receiving location updates whenever the Activity becomes invisible.
|
||||
@Override
|
||||
protected void onStop() {
|
||||
@@ -338,4 +370,24 @@ public class LocationActivity extends Activity {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Dialog to prompt users to enable GPS on the device.
|
||||
*/
|
||||
private class EnableGpsDialogFragment extends DialogFragment {
|
||||
|
||||
@Override
|
||||
public Dialog onCreateDialog(Bundle savedInstanceState) {
|
||||
return new AlertDialog.Builder(getActivity())
|
||||
.setTitle(R.string.enable_gps)
|
||||
.setMessage(R.string.enable_gps_dialog)
|
||||
.setPositiveButton(R.string.enable_gps, new DialogInterface.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
enableLocationSettings();
|
||||
}
|
||||
})
|
||||
.create();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||