Location service and LocationManager : Location « Hardware « Android






Location service and LocationManager

 

package app.Test;

import java.util.List;

import android.app.Activity;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;
import android.widget.TextView;

public class appTest extends Activity implements LocationListener {

  private static final String[] A = { "n/a", "fine", "coarse" };
  private static final String[] P = { "n/a", "low", "medium", "high" };
  private static final String[] S = { "out of service","temporarily unavailable", "available" };

  private LocationManager mgr;
  private TextView output;
  private String best;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mgr = (LocationManager) getSystemService(LOCATION_SERVICE);
    output = (TextView) findViewById(R.id.output);

    log("Location providers:");
    List<String> providers = mgr.getAllProviders();
    for (String provider : providers) {
      dumpProvider(provider);
    }

    Criteria criteria = new Criteria();
    best = mgr.getBestProvider(criteria, true);
    log("\nBest provider is: " + best);

    Location location = mgr.getLastKnownLocation(best);
    dumpLocation(location);
  }
  @Override
  protected void onResume() {
    super.onResume();
    mgr.requestLocationUpdates(best, 15000, 1, this);
  }
  @Override
  protected void onPause() {
    super.onPause();
    mgr.removeUpdates(this);
  }

  public void onLocationChanged(Location location) {
    dumpLocation(location);
  }

  public void onProviderDisabled(String provider) {
    log("\nProvider disabled: " + provider);
  }

  public void onProviderEnabled(String provider) {
    log("\nProvider enabled: " + provider);
  }

  public void onStatusChanged(String provider, int status, Bundle extras) {
    log("\nProvider status changed: " + provider + ", status=" + S[status]
        + ", extras=" + extras);
  }

  private void log(String string) {
    output.append(string + "\n");
  }

  private void dumpProvider(String provider) {
    LocationProvider info = mgr.getProvider(provider);
    StringBuilder builder = new StringBuilder();
    builder.append("LocationProvider[").append("name=")
        .append(info.getName()).append(",enabled=")
        .append(mgr.isProviderEnabled(provider))
        .append(",getAccuracy=").append(A[info.getAccuracy()])
        .append(",getPowerRequirement=")
        .append(P[info.getPowerRequirement()])
        .append(",hasMonetaryCost=").append(info.hasMonetaryCost())
        .append(",requiresCell=").append(info.requiresCell())
        .append(",requiresNetwork=").append(info.requiresNetwork())
        .append(",requiresSatellite=").append(info.requiresSatellite())
        .append(",supportsAltitude=").append(info.supportsAltitude())
        .append(",supportsBearing=").append(info.supportsBearing())
        .append(",supportsSpeed=").append(info.supportsSpeed())
        .append("]");
    log(builder.toString());
  }

  private void dumpLocation(Location location) {
    if (location == null)
      log("\nLocation[unknown]");
    else
      log("\n" + location.toString());
  }

}

//main.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent">
   <TextView
      android:id="@+id/output"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content" />
</ScrollView>

   
  








Related examples in the same category

1.Location service
2.Using LocationManager
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