import androidx.appcompat.app.AppCompatActivity;import android.Manifest;import android.app.Activity;import android.content.Context;import android.content.pm.PackageManager;import android.location.Location;import android.location.LocationListener;import android.location.LocationManager;import android.os.Bundle;import android.util.Log;import android.view.View;import android.widget.Button;import android.widget.TextView;import android.widget.Toast;import java.util.List;public class MainActivity extends Activity {    private LocationManager locationManager;    private String locationProvider;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        //获取地理位置管理器        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);        //获取所有可用的位置提供器        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).show();            return;        }        //获取Location        if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {            // TODO: Consider calling            //    Activity#requestPermissions            // here to request the missing permissions, and then overriding            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,            //                                          int[] grantResults)            // to handle the case where the user grants the permission. See the documentation            // for Activity#requestPermissions for more details.            return;        }        Location location = locationManager.getLastKnownLocation(locationProvider);        if (location != null) {            //不为空,显示地理位置经纬度            Log.i("TAG", "经度" + location.getLongitude() + "纬度" + location.getLatitude());            TextView tv1 = findViewById(R.id.tv1);            tv1.setText("经度" + location.getLongitude() + "纬度" + location.getLatitude());        }        //监视地理位置变化        if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {            // TODO: Consider calling            //    Activity#requestPermissions            // here to request the missing permissions, and then overriding            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,            //                                          int[] grantResults)            // to handle the case where the user grants the permission. See the documentation            // for Activity#requestPermissions for more details.            return;        }        locationManager.requestLocationUpdates(locationProvider, 1000, 1, 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) {            //如果位置发生变化,重新显示 如果位置改变,经纬度没有变,不会执行此函数 应该说经纬度发生变化执行此函数            Log.i("TAG","经度"+location.getLongitude()+"纬度"+location.getLatitude());        }    };    @Override    protected void onDestroy() {        super.onDestroy();        if (locationManager != null) {            //移除监听器            locationManager.removeUpdates(locationListener);        }    }}

更多相关文章

  1. Android根据经纬度获取位置信息
  2. android 通过wifi 获取经纬度和获取渠道号/屏幕宽高
  3. Android 高德地图点击地图获取经纬度
  4. Android原生方式获取经纬度和城市信息
  5. Android获取经纬度
  6. Android之根据经纬度查询位置地址名称
  7. Android开发者已经度过了初级吗
  8. Android 获取经纬度。2018年写
  9. Android中通过经纬度来过去到城市名称

随机推荐

  1. Mysql异常_01_ 誓死登进mysql_Can&#39;t
  2. qt连接mysql运行时出现QSqlQuery::exec:d
  3. 【Qt】Qt5.12连接MySQl5.7(亲自测试成功)
  4. MySQL DELETE语句和TRUNCATE TABLE语句的
  5. 计算mysql中每天的发生次数
  6. 为获得快速结果,我的查询有什么好的索引?
  7. 读《SQL优化核心思想》:你不知道的优化技
  8. Android如何通过JDBC直连访问MySQL数据库
  9. MySQL各模块工作配合
  10. mysql主从同步(4)-Slave延迟状态监控