LocationTest.java
上传用户:xmjingguan
上传日期:2009-07-06
资源大小:2054k
文件大小:4k
源码类别:

android开发

开发平台:

Java

  1. /***
  2.  * Excerpted from "Hello, Android!",
  3.  * published by The Pragmatic Bookshelf.
  4.  * Copyrights apply to this code. It may not be used to create training material, 
  5.  * courses, books, articles, and the like. Contact us if you are in doubt.
  6.  * We make no guarantees that this code is fit for any purpose. 
  7.  * Visit http://www.pragmaticprogrammer.com/titles/eband for more book information.
  8. ***/
  9. package org.example.locationtest;
  10. import java.util.List;
  11. import android.app.Activity;
  12. import android.location.Criteria;
  13. import android.location.Location;
  14. import android.location.LocationListener;
  15. import android.location.LocationManager;
  16. import android.location.LocationProvider;
  17. import android.os.Bundle;
  18. import android.widget.TextView;
  19. public class LocationTest extends Activity implements
  20.       LocationListener { 
  21.    
  22.    private static final String[] A = { "n/a", "fine", "coarse" };
  23.    private static final String[] P = { "n/a", "low", "medium",
  24.          "high" };
  25.    private static final String[] S = { "out of service",
  26.          "temporarily unavailable", "available" };
  27.    
  28.    private LocationManager mgr;
  29.    private TextView output;
  30.    private String best;
  31.    @Override
  32.    public void onCreate(Bundle savedInstanceState) {
  33.       super.onCreate(savedInstanceState);
  34.       setContentView(R.layout.main);
  35.       mgr = (LocationManager) getSystemService(LOCATION_SERVICE); 
  36.       output = (TextView) findViewById(R.id.output);
  37.       log("Location providers:");
  38.       dumpProviders(); 
  39.       Criteria criteria = new Criteria(); 
  40.       best = mgr.getBestProvider(criteria, true);
  41.       log("nBest provider is: " + best);
  42.       log("nLocations (starting with last known):");
  43.       Location location = mgr.getLastKnownLocation(best); 
  44.       dumpLocation(location);
  45.    }
  46.    
  47.    
  48.    @Override
  49.    protected void onResume() {
  50.       super.onResume();
  51.       // Start updates (doc recommends delay >= 60000 ms)
  52.       mgr.requestLocationUpdates(best, 15000, 1, this);
  53.    }
  54.    @Override
  55.    protected void onPause() {
  56.       super.onPause();
  57.       // Stop updates to save power while app paused
  58.       mgr.removeUpdates(this);
  59.    }
  60.    
  61.    
  62.    public void onLocationChanged(Location location) {
  63.       dumpLocation(location);
  64.    }
  65.    public void onProviderDisabled(String provider) {
  66.       log("nProvider disabled: " + provider);
  67.    }
  68.    public void onProviderEnabled(String provider) {
  69.       log("nProvider enabled: " + provider);
  70.    }
  71.    public void onStatusChanged(String provider, int status,
  72.          Bundle extras) {
  73.       log("nProvider status changed: " + provider + ", status="
  74.             + S[status] + ", extras=" + extras);
  75.    }
  76.    
  77.    private void log(String string) {
  78.       output.append(string + "n");
  79.    }
  80.    private void dumpProviders() {
  81.       List<String> providers = mgr.getAllProviders();
  82.       for (String provider : providers) {
  83.          dumpProvider(provider);
  84.       }
  85.    }
  86.    private void dumpProvider(String provider) {
  87.       LocationProvider info = mgr.getProvider(provider);
  88.       StringBuilder builder = new StringBuilder();
  89.       builder.append("LocationProvider[")
  90.             .append("name=")
  91.             .append(info.getName())
  92.             .append(",enabled=")
  93.             .append(mgr.isProviderEnabled(provider))
  94.             .append(",getAccuracy=")
  95.             .append(A[info.getAccuracy()])
  96.             .append(",getPowerRequirement=")
  97.             .append(P[info.getPowerRequirement()])
  98.             .append(",hasMonetaryCost=")
  99.             .append(info.hasMonetaryCost())
  100.             .append(",requiresCell=")
  101.             .append(info.requiresCell())
  102.             .append(",requiresNetwork=")
  103.             .append(info.requiresNetwork())
  104.             .append(",requiresSatellite=")
  105.             .append(info.requiresSatellite())
  106.             .append(",supportsAltitude=")
  107.             .append(info.supportsAltitude())
  108.             .append(",supportsBearing=")
  109.             .append(info.supportsBearing())
  110.             .append(",supportsSpeed=")
  111.             .append(info.supportsSpeed())
  112.             .append("]");
  113.       log(builder.toString());
  114.    }
  115.    private void dumpLocation(Location location) {
  116.       if (location == null)
  117.          log("nLocation[unknown]");
  118.       else
  119.          log("n" + location.toString());
  120.    }
  121.    
  122. }