0

我已经编写了代码,如果 GPS 被禁用,它将由代码启用并尝试从 gps 获取位置,但我得到一个空值。下面是我的代码

   public void getValue() {
    LocationManager mlocManager = (LocationManager) MySettings.this.getSystemService(Context.LOCATION_SERVICE);
    boolean gpsEnabled = mlocManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    System.out.println("GPS IS "+gpsEnabled);
    if (!gpsEnabled) {
        String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);

        if (!provider.contains("gps")) { // if gps is disabled
            final Intent poke = new Intent();
            poke.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider");
            poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
            poke.setData(Uri.parse("3"));
            sendBroadcast(poke);
        }
    }
    SimpleDateFormat sdfDate = new SimpleDateFormat("MM/dd/yyyy");
    try {
        getBatteryLevel();
        mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, MySettings.this);
        Location location = mlocManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        mLocation = location;
        if (location != null) {
            lat = location.getLatitude();
            lon = location.getLongitude();
            address = getAddress();
            alt = location.getAltitude();
            if (meterFootFlag) {
                diameter = location.getAccuracy();
            } else
                diameter = location.getAccuracy() / 3.28084;
        } else {
            lat = 0.0;
            lon = 0.0;
            alt = 0.0;
        }

    } catch (Exception e) {
        lat = 0.0;
        lon = 0.0;
        alt = 0.0;
    }

我还在清单文件中添加了权限

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

但我得到了该位置的空值。

关于如何获得位置的任何想法?

4

5 回答 5

2

您的代码是正确的,只需等到 GP 从卫星获取高度可能需要不到 1 分钟的时间。

于 2013-11-14T12:57:14.890 回答
0

确保您只检查DEVICE..在模拟器中,它会在系统中运行时给出空值,GPS因此它没有权限GPS

于 2012-10-11T13:20:23.487 回答
0

尝试:

    public Location showLocation(){
    LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    //Class model, save latitude and longitude
    NavegadorCoordenadas locate = new NavegadorCoordenadas();
    Criteria crit = new Criteria();
    crit.setAccuracy(Criteria.ACCURACY_FINE);
    String provider = lm.getBestProvider(crit, false);
    Location loc = getLastKnownLocation(lm);                    
    locate.setLatitude(loc.getLatitude()); 
    locate.setLongitude(loc.getLongitude());
    return loc;     
}

private Location getLastKnownLocation(LocationManager location) {
    List<String> providers = location.getProviders(true);
    Location bestLocation = null;
    for (String provider : providers) {
        Location l = location.getLastKnownLocation(provider);           
        if (l == null) {
            continue;
        }
        if (bestLocation == null || l.getAccuracy() < bestLocation.getAccuracy()) {             
            bestLocation = l;
        }
    }
    if (bestLocation == null) {
        return null;
    }
    return bestLocation;
}
于 2012-10-11T13:15:42.117 回答
0
locationMangaer = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
        if(!provider.contains("gps")){ //if gps is disabled
            final Intent poke = new Intent();
            poke.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider"); 
            poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
            poke.setData(Uri.parse("3")); 
            sendBroadcast(poke);
        }

    locationListener = new MyLocationListener();
    locationMangaer.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10, 10,
            locationListener);

现在在同一活动中创建MyLocationListener 类。

private class MyLocationListener implements LocationListener {
    @Override
    public void onLocationChanged(Location loc) {

            String longitude = "Longitude: " +loc.getLongitude();  
            String latitude = "Latitude: " +loc.getLatitude();

            /*----------to get City-Name from coordinates ------------- */
            String cityName=null;                 
            Geocoder gcd = new Geocoder(getBaseContext(), Locale.getDefault());                  
            List<Address>  addresses;  
            try {  
             addresses = gcd.getFromLocation(loc.getLatitude(), loc.getLongitude(), 1);  
             if (addresses.size() > 0) { 
              System.out.println(addresses.get(0).getLocality());  
             cityName=addresses.get(0).getLocality();  
                System.out.println("MESSAGE:"+cityName);
             }
            } catch (IOException e) {                 
             e.printStackTrace();  
            } 

            String s = longitude+"\n"+latitude+"\t city name:"+cityName;
                Log.v("OUTPUT, s);
    }

    @Override
    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub          
    }

    @Override
    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub          
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub          
    }
}

在实际设备中运行您的应用程序。

您可以弄清楚自动启动GPS并查看控制台logcat。

于 2013-04-03T07:12:19.327 回答
0

确保通过 Settings--> Applications-->YourApp-->permissions 打开 Permissions 另一个原因可能是延迟,连接网络需要时间,有时超过一分钟

于 2017-06-04T13:59:24.460 回答