我如何检查是否GPS被启用之前,我尝试使用它 [英] How can I check if GPS is enabled before I try to use it

查看:188
本文介绍了我如何检查是否GPS被启用之前,我尝试使用它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的code,这不是一件好事,因为有时候GPS需要很长

我如何能做到以下几点:

  1. 检查GPS启用
  2. 使用GPS,如果它被启用以其他方式使用的网络提供商。
  3. 如果GPS时间超过30秒,请使用网络。

我可以用时间或视频下载与我自己的逻辑做到这一点,但我觉得可能是一个更stabndard方式

  //获取一个参考系统位置管理
            LocationManager locationManager =(LocationManager)this.getSystemService(Context.LOCATION_SERVICE);

            //定义一个监听器,响应位置更新
            LocationListener的LocationListener的=新LocationListener的(){
                公共无效onLocationChanged(位置定位){
                  //当一个新的位置由网络位置提供商发现调用。
                    locationCallback(位置);
                }

                公共无效onStatusChanged(字符串商,INT地位,捆绑演员){}

                公共无效onProviderEnabled(字符串提供商){}

                公共无效onProviderDisabled(字符串提供商){}
              };

            //注册监听器的位置管理器来接收位置更新
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,LocationListener的);
 

解决方案

有没有标准的方式,你必须做你自己的帮助下:

 如果(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
        //你需要什么,如果启用了...
    }其他{
        //你需要的,如果没有启用什么?
    }
 

和清单中此权限:

 <使用-权限的Andr​​oid:名称=android.permission.ACCESS_FINE_LOCATION/>
 

由于如果未启用GPS的推荐,通常是标准规定,弹出位置设置的活动,使用户可以专门启用它...

希望这有助于。

商祺!

I have the following code and it's not good because sometimes GPS takes very long

How can I do the following:

  1. Check if GPS is enabled
  2. Use GPS if it is enabled otherwise use the network provider.
  3. If GPS takes more than 30 seconds, use network.

I can do this with my own logic using a time or Thread.sleep but I think there might be a more stabndard way

// Acquire a reference to the system Location Manager
            LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

            // Define a listener that responds to location updates
            LocationListener locationListener = new LocationListener() {
                public void onLocationChanged(Location location) {
                  // Called when a new location is found by the network location provider.
                    locationCallback(location);
                }

                public void onStatusChanged(String provider, int status, Bundle extras) {}

                public void onProviderEnabled(String provider) {}

                public void onProviderDisabled(String provider) {}
              };

            // Register the listener with the Location Manager to receive location updates
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);

解决方案

There's no standard way, you have to do it on your own with the help of:

    if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
        //Do what you need if enabled...
    }else{
        //Do what you need if not enabled...
    }

And this permission in manifest:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

As recommendation if GPS is not enabled, usually the standard specifies to popup the Location Settings Activity so the user can specifically enable it...

Hope this helps.

Regards!

这篇关于我如何检查是否GPS被启用之前,我尝试使用它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆