Using LocationManager : Location « Hardware « Android






Using LocationManager

 

package app.test;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.widget.TextView;

public class Test extends Activity {
  LocationManager manager;
  Location currentLocation;
  String selectedProvider;

  TextView locationView;

  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    locationView = new TextView(this);
    setContentView(locationView);
    manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
  }
  @Override
  public void onResume() {
    super.onResume();
    if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
      AlertDialog.Builder builder = new AlertDialog.Builder(this);
      builder.setTitle("Location Manager");
      builder.setMessage("GPS is currently disabled.\nWould you like to change these settings now?");
      builder.setPositiveButton("Yes",
          new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
              Intent i = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
              startActivity(i);
            }
          });
      builder.setNegativeButton("No",
          new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
              finish();
            }
          });
      builder.create().show();
    }
    currentLocation = manager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    updateDisplay();
    int minTime = 5000;
    float minDistance = 0;
    manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, minTime,
        minDistance, listener);
  }

  @Override
  public void onPause() {
    super.onPause();
    manager.removeUpdates(listener);
  }

  private void updateDisplay() {
    if (currentLocation == null) {
      locationView.setText("Determining Your Location...");
    } else {
      locationView.setText(String.format("Your Location:\n%.2f, %.2f",
          currentLocation.getLatitude(),
          currentLocation.getLongitude()));
    }
  }
  private LocationListener listener = new LocationListener() {
    @Override
    public void onLocationChanged(Location location) {
      currentLocation = location;
      updateDisplay();
    }

    @Override
    public void onProviderDisabled(String provider) {
    }

    @Override
    public void onProviderEnabled(String provider) {
    }

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

  };
}

   
  








Related examples in the same category

1.Location service and LocationManager
2.Location service
3.My location
4.Display GEO location
5.Using location service for the weather
6.Using Intent to go to a geo location
7.Location based service
8.My location and Google Map
9.Custom Location Overlay
10.Get my location
11.Geo location and Google Map
12.Location Tracking
13.A light pool of objects that can be resused to avoid allocation.
14.extends android.location.Location
15.Geo Location Util
16.C:\Java_Dev\WEB\dev\android\weatherforecastsystem-read-only\com\hci\pwf\LocationUtil.java
17.upload Data with Geo location
18.Copy a file from one location to another.
19.LocationManager.GPS_PROVIDER
20.Location util