Android获取用户位置一次

时间:2017-09-06 11:13:21

标签: java android location android-gps

我知道周围有很多东西,但即使我读完所有内容(差不多),我仍然无法弄清楚问题。

基本上我想在打开应用程序或按下按钮并保存在共享首选项上时让用户获得精细位置。 我的旧代码有效,但由于获取坐标的速度很慢,因此无法保存共享偏好(GPS图标不会显示在条形图上)。

OLD:

public void askLocation() {
    if (ContextCompat.checkSelfPermission(MainActivity.this,                                                 Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED)         {
        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_COARSE);
        criteria.setPowerRequirement(Criteria.POWER_LOW);
        criteria.setAltitudeRequired(false);
        criteria.setBearingRequired(false);
        criteria.setSpeedRequired(false);
        criteria.setCostAllowed(true);
        criteria.setHorizontalAccuracy(Criteria.ACCURACY_HIGH);
        criteria.setVerticalAccuracy(Criteria.ACCURACY_HIGH);

        // Now create a location manager
        LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        Looper looper = null;
        locationManager.requestSingleUpdate(criteria, locationListener, looper);

        String currentCoordinates = somename.getString("LastLocation", "0,0");
        Log.d("SomeName", "lastLocation askLocation: " + currentCoordinates);
    }
}

final LocationListener locationListener = new LocationListener() {
    @Override
    public void onLocationChanged(Location location) {
        mLocation = location;
        String currentCoordinates = somename.getString("LastLocation", "0,0");

        if (location != null) {
            double currentLatitude = location.getLatitude();
            double currentLongitude = location.getLongitude();

            currentCoordinates = currentLatitude + "," + currentLongitude;
        }

        SharedPreferences.Editor editor = somename.edit();
        editor.putString("LastLocation", currentCoordinates).commit();
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        Log.d("Status Changed", String.valueOf(status));
    }

    @Override
    public void onProviderEnabled(String provider) {
        Log.d("Provider Enabled", provider);
    }

    @Override
    public void onProviderDisabled(String provider) {
        Log.d("Provider Disabled", provider);
    }
};

从Stackoverflow上的帖子中使用的我的新代码未正确显示(条形图上显示GPS图标):

default submit

谢谢。

1 个答案:

答案 0 :(得分:0)

您可以尝试使用此方法在打开应用程序或按下按钮并保存在共享首选项上时获取一次该位置。

               if (Constants.isOnline(getApplicationContext())) {
                        gps = new GPSTracker(v.getContext(),this);
                        if (gps.canGetLocation()) {

                            latitude = gps.getLatitude();
                            longitude = gps.getLongitude();
                            Log.e("Location ", "latitude : " + latitude + "longitude :" + longitude);

                        } else {
                            gps.showSettingsAlert();
                        }
                }

isOnline()是检查用户是否有intenet连接。

 public static boolean isOnline(Context c) {
    ConnectivityManager cm = (ConnectivityManager) c.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();
    if (netInfo != null && netInfo.isConnectedOrConnecting()) {
        return true;
    } else {
        Toast.makeText(c, "No Internet Connection.", Toast.LENGTH_SHORT).show();
    }
return false;

}

相关问题