content: package com.share.jack.customviewpath;
  
  import android.Manifest;
  import android.app.Activity;
  import android.content.pm.PackageManager;
  import android.location.Location;
  import android.location.LocationListener;
  import android.location.LocationManager;
  import android.os.Bundle;
  import android.support.v4.app.ActivityCompat;
  import android.util.Log;
  import android.view.View;
  import android.widget.TextView;
  import android.widget.Toast;
  
  import java.util.List;
  
  /**
   *
   */
  
  public class LocationActivity extends Activity {
  
      private TextView tvLocation;
      private Location mLocation;
  
      private LocationManager locationManager;
      private String locationProvider;
  
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_location);
          locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
          tvLocation = (TextView) findViewById(R.id.al_tv_location);
  
          List<String> providers = locationManager.getProviders(true);
          if (providers.contains(LocationManager.GPS_PROVIDER)) {
              //如果是GPS
              locationProvider = LocationManager.GPS_PROVIDER;
          } else if (providers.contains(LocationManager.NETWORK_PROVIDER)) {
              //如果是Network
              locationProvider = LocationManager.NETWORK_PROVIDER;
          } else {
              Toast.makeText(this, "没有可用的位置提供器", Toast.LENGTH_SHORT);
              return;
          }
  
          if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
                  && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
              return;
          }
          mLocation = locationManager.getLastKnownLocation(locationProvider);
          /**
           * LocationListern监听器
           * 参数：地理位置提供器、监听位置变化的时间间隔、位置变化的距离间隔、LocationListener监听器
           */
  
          LocationListener locationListener = new LocationListener() {
  
              @Override
              public void onStatusChanged(String provider, int status, Bundle arg2) {
  
              }
  
              @Override
              public void onProviderEnabled(String provider) {
  
              }
  
              @Override
              public void onProviderDisabled(String provider) {
  
              }
  
              @Override
              public void onLocationChanged(Location location) {
                  //如果位置发生变化,重新显示
                  mLocation = location;
                  showLocation();
              }
          };
          //监视地理位置变化
          locationManager.requestLocationUpdates(locationProvider, 3000, 1, locationListener);
  
          findViewById(R.id.al_btn_get_location).setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  if (null != mLocation) {
                      showLocation();
                  }
              }
          });
  
      }
  
      private void showLocation() {
          Log.d("定位成功------->", "location------>" + "经度为：" + mLocation.getLongitude() + "\n纬度为：" + mLocation.getLatitude());
          tvLocation.setText("location------>" + "经度为：" + mLocation.getLongitude() + "\n纬度为：" + mLocation.getLatitude());
      }
  }
  