获取LocationProvider的三种方法

一、获取所有的LocationProvider并用TextView显示出来

        //获取显示LocationProvider名称的TextView        providerTv = findViewById(R.id.act_provider_tv);        //获取位置服务        LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);        //这个方法返回值是List集合,里面数据是String类型的        List providerNames = locationManager.getAllProviders();//获取所有的LocationProvider名称        StringBuilder stringBuilder = new StringBuilder();//字符构建器        for ( Iterator iterator = providerNames.iterator();iterator.hasNext();) {            stringBuilder.append(iterator.next() + "\n");        }        providerTv.setText(stringBuilder.toString());

二、通过名称获得LocationProvider

        //获取显示LocationProvider名称的TextView        providerTv = findViewById(R.id.act_provider_tv);        //获取位置服务        LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);        //获取基于GPS的LocationProvider        //需要加入权限          LocationProvider provider = locationManager.getProvider(LocationManager.GPS_PROVIDER);        providerTv.setText(provider.getName());

三、通过Criteria类获得LocationProvider

        //获取显示LocationProvider名称的TextView        providerTv = findViewById(R.id.act_provider_tv);        //获取位置服务        LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);        //获取最佳的LocationProvider        //创建一个过滤条件对象        //需要加入权限          Criteria criteria = new Criteria();        //设置为不收费的        criteria.setCostAllowed(false);        //使用精度最准确的        criteria.setAccuracy(Criteria.ACCURACY_FINE);        //设置中等耗电量        criteria.setPowerRequirement(Criteria.POWER_MEDIUM);        //获取最佳的LocationProvider名称        String provider = locationManager.getBestProvider(criteria, true);        providerTv.setText(provider);

——————————————————————————————————————————————————————————

获取定位信息

  • 获取一个locationManager对象,主要通过getSystemService获取
  • 设置一个监听器,用来实现每隔一段时间获取定位信息
  • 编写一个locationUpdates方法,把获取到的定位信息进行输出
  • 获取locationManager对象的getLastKnownLocation方法获取最新的定位信息
  • 传到locationUpdates方法中
private TextView locationTv;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,                WindowManager.LayoutParams.FLAG_FULLSCREEN);     //设置全屏        //获取显示LocationProvider名称的TextView        locationTv = findViewById(R.id.act_title_tv);        //获取位置服务        LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);        //权限检查,编辑器自动添加        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {            // TODO: Consider calling            //    ActivityCompat#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 ActivityCompat#requestPermissions for more details.            return;        }        assert locationManager != null;        locationManager.requestLocationUpdates(                LocationManager.GPS_PROVIDER,   //指定GPS定位的提供者                1000,                 //间隔时间                1,                 //位置间隔1米                new LocationListener() {//监听GPS定位信息是否改变                    @Override                    public void onLocationChanged(Location location) {                    //GPS信息发生改变时,回调                    }                    @Override                    public void onStatusChanged(String provider, int status, Bundle extras) {                        //GPS状态发生改变时,回调                    }                    @Override                    public void onProviderEnabled(String provider) {                    //定位提供者启动时回调                    }                    @Override                    public void onProviderDisabled(String provider) {                        //定位提供者关闭时回调                    }                }        );        //获取最新的定位信息        Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);        //将最新的定位信息传递给locationUpdates()方法        locationUpdates(location);//              需要添加两个位置权限//              近似精度的权限//            //              更精细精度的访问权限//                }    public void locationUpdates(Location location) {        if (location != null) {            //创建一个字符串构建器,用于记录位置信息            StringBuilder stringBuilder = new StringBuilder();            stringBuilder.append("您的位置是: \n");            stringBuilder.append("经度: ");            stringBuilder.append(location.getLongitude());            stringBuilder.append("\n 纬度:");            stringBuilder.append(location.getLatitude());            locationTv.setText(stringBuilder.toString());        } else {            locationTv.setText("没有获取到GPS信息");        }    }

 

 

更多相关文章

  1. android 动态增加不同名称的按钮
  2. 获取Android(安卓)IP信息
  3. 天气预报(二)
  4. Android获取当前位置(GPS和网络定位)
  5. android 音量调节时,嘟 的 发声位置
  6. android 获取Service(服务)的运行状态
  7. android I/DEBUG堆栈信息
  8. Android(安卓)常见工具类封装
  9. 如何把应用安装到sd卡中

随机推荐

  1. 扫雷游戏
  2. 新人
  3. 坚果云和亿方云相比较哪个好?
  4. 坚果云亿方云哪个比较好
  5. 坚果云和亿方云哪个更适合公司?
  6. Vue自学之路5-vue模版语法(v-text,v-html,
  7. DEFI去中心化金融的机会在哪里?
  8. 笔记本硬盘被格式化了的资料寻回办法
  9. 谈谈认知宽度
  10. 技术架构如何抓大放小