【问题标题】:How to gain permission to get GPS location in Android如何在 Android 中获得获取 GPS 位置的权限
【发布时间】:2020-12-17 13:33:26
【问题描述】:

我是 Android 新手,在我当前的项目中,我正在尝试使用 GPS 检索位置。获取位置的代码在单独的非活动类中,因为我显示坐标的片段被塞满了。

我尝试使用 developer.android.com 中的指南,但我无法获得任何权限提示,并且当我按下片段中的按钮时,我只能获得纬度和经度的默认 0 值。

我已经在清单中提供了权限:

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

GPS 定位器类:

public class LocationProvider implements ActivityCompat.OnRequestPermissionsResultCallback {
    private static final int PERMISSION_REQUEST_LOCATION = 1;
    private Activity activity;

    Location gps_loc;
    Location final_loc;
    double longitude;
    double latitude;
    private LocationManager locationManager;

    public LocationProvider(Activity activity) {
        this.activity =activity;
        locationManager = (LocationManager) activity.getSystemService(Context.LOCATION_SERVICE);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (requestCode == PERMISSION_REQUEST_LOCATION) {
            // Request for permission.
            if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                getLocation();
            } else {
                requestLocationPermission();
            }
        }
    }

    private void getLocation() {
        // Check if the permission has been granted
        if (ActivityCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_FINE_LOCATION)
                == PackageManager.PERMISSION_GRANTED) {
            gps_loc = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
            if (gps_loc != null) {
                Log.v("Loc_provider",gps_loc.toString());
                final_loc = gps_loc;
                latitude = final_loc.getLatitude();
                longitude = final_loc.getLongitude();
            }
        } else {
            requestLocationPermission();
        }
    }


    private void requestLocationPermission() {
            ActivityCompat.requestPermissions(activity,
                    new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, PERMISSION_REQUEST_LOCATION);
        }


    public String getCoordinates() {
        return latitude + " "+ longitude;
    }
}

片段中的代码:

FloatingActionButton fab=view.findViewById(R.id.floatingActionButton);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                locProvider=new LocationProvider(requireActivity());
                gpsCoordinates.setText(locProvider.getCoordinates());
            }
        });

非常欢迎修复代码的提示和解决方案。

更新 1: 我移动了片段中的代码,使其如下所示:

fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!checkIfAlreadyhavePermission()) {
                    requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
                } else {
                    gps_loc = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                    gpsCoordinates.setText(getCoordinates());
                    }
                }
        });

private String getCoordinates() {
        if (gps_loc != null) {
            Log.v("Loc_provider", gps_loc.toString());
            final_loc = gps_loc;
            latitude = gps_loc.getLatitude();
            longitude = gps_loc.getLongitude();
        }
        return latitude + " " + longitude;
    }

    private boolean checkIfAlreadyhavePermission() {
        int result = ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION);
        return result == PackageManager.PERMISSION_GRANTED;
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            case PERMISSION_REQUEST_LOCATION: {
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    //Log.v("Grant_Results", String.valueOf(grantResults[0]));
                    Toast.makeText(getActivity(), "permission granted", Toast.LENGTH_LONG).show();
                    gpsCoordinates.setText(getCoordinates());
                    } else {
                       Toast.makeText(getActivity(), "permission denied", Toast.LENGTH_LONG).show();
                    }
                break;
                }
            }
    }

我现在得到权限提示,但位置仍然没有更新(我得到 0)。

【问题讨论】:

  • 只删除ACCESS 前的空格。 ACCESS_COARSE_LOCATION" />
  • 不幸的是没有任何效果。

标签: android android-permissions android-gps


【解决方案1】:

请注意:

  1. 不建议保存 Activity 对象,这可能会导致内存泄漏。 Activity 可能会被销毁并重新创建(例如,当屏幕方向改变时)。持有对旧活动的引用会给您带来麻烦。

  2. 实现ActivityCompat.OnRequestPermissionsResultCallback 接口什么都不做。你的方法永远不会被调用。您必须从活动中显式调用它。

  3. 您尚未请求权限,因此您的权限提示将永远不会显示,并且将返回默认值 double 即 0。您需要调用 getLocation() 方法,以便控件落在 else 块上并显示您的权限提示

我建议您在活动中处理权限。让活动获取该位置的坐标。您可以使用方法getCoordinates() 让活动实现一个接口,例如LocationFetcher。然后你可以像这样在片段中调用它:

LocationFetcher locationFetcher = (LocationFetcher) activity;
gpsCoordinates.setText(locationFetcher.getCoordinates());

【讨论】:

  • 如果你明白我想说的话,请告诉我。如果没有,我会帮你写代码
  • 谢谢。我已经尝试在 onClick()- 方法中实现代码。如果我卸载应用程序并重新安装,我会收到权限请求,但是我仍然没有更新坐标,我只得到 0。你能帮忙吗?
  • 现在已修复。 .
【解决方案2】:

通过在 Fragment 中实现 LocationListener 然后添加来修复

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 400, 1, this);

在 onRequestPermissionsResult() 中。

【讨论】:

    猜你喜欢
    • 2011-05-04
    • 2013-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-17
    • 1970-01-01
    相关资源
    最近更新 更多