Search This Blog

Sunday 24 June 2012

Implementing the location aware apps on Android



Android provide the platform to build the location aware applications. You can implement the location based application using the android.location package classes.

The core of the android location framework is Location Manager. You can request the location manager service from system using the getSystemService API call.

See below the implementation for the activity requesting the location of the user from GPS location provider.

//create the object for the location manager
private LocationManager mLocationManager;
//create the object for the GPS location
private Location gpsLocation;
//frequency for location determination
private static final int TEN_SECONDS = 10000;
//distance for location determination
private static final int TEN_METERS = 10;

// Get a reference to the LocationManager object.
mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

Now implement a check for the location services are enabled on the device or not.

    protected void onStart() {
        super.onStart();

        // Check if the GPS setting is currently enabled on the device.
        // This check should be implement in onStart Method as the system calls this method every time the activity resume
        LocationManager locationManager =
                (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        final boolean gpsProvider = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

        if (!gpsProvider) {
            // Create an alert dialog to request user to enable the location services
            new EnableGpsDialogFragment().show(getSupportFragmentManager(), "enableGpsDialog");
        }
    }


Now add the following method to call the settings interface using the intents (Start another activity)

  // Method to launch Settings interface
    private void enableLocationSettings() {
        Intent settingsIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
        startActivity(settingsIntent);
    }

Now implement the dialog interface for user to choose the option to enable the location services

/**
     * 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();
        }
    }

Now request the location provider from location manager

requestUpdatesFromProvider(
                    LocationManager.GPS_PROVIDER, R.string.not_support_gps);

Call this method upon the activity resume by the user.

 @Override
    protected void onResume() {
        super.onResume();
        gpsLocation = requestUpdatesFromProvider();
    }

Now implement the above method to register the provider

private Location requestUpdatesFromProvider(final String provider, final int errorResId) {
        Location location = null;
        if (mLocationManager.isProviderEnabled(provider)) {
            mLocationManager.requestLocationUpdates(provider, TEN_SECONDS, TEN_METERS, listener);
            location = mLocationManager.getLastKnownLocation(provider);
        } else {
            Toast.makeText(this, errorResId, Toast.LENGTH_LONG).show();
        }
        return location;
    }

Now create the location listener with the handler methods

private final LocationListener listener = new LocationListener() {

        @Override
        public void onLocationChanged(Location location) {
            // A new location update is received.  Do something useful with it.  Update the UI with
            // here you will get the location update which you can update in UI
            
        }

        @Override
        public void onProviderDisabled(String provider) {
        }

        @Override
        public void onProviderEnabled(String provider) {
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
        }
    };

Make sure that you will remove the location updates from activity on onStop method

  // Stop receiving location updates whenever the Activity becomes invisible.
    @Override
    protected void onStop() {
        super.onStop();
        mLocationManager.removeUpdates(listener);
    }

Create your first app to get your location and enjoy with Android.










No comments:

Post a Comment