如何将 GeoLocationClass 的权限检查到 MainActivity 中?

How can I check Permission for GeoLocationClass into MainActivity?

我正在对

进行权限检查
locationManager.requestLocationUpdates(
                LocationManager.GPS_PROVIDER, LOCATION_PROVIDER_UPDATE_RATE,
                LOCATION_PROVIDER_UPDATE_DISTANCE, locationListener);

        locationManager.requestLocationUpdates(
                LocationManager.NETWORK_PROVIDER,
                LOCATION_PROVIDER_UPDATE_RATE,
                LOCATION_PROVIDER_UPDATE_DISTANCE, locationListener);
    }

---------------------------

}
        locationManager.removeUpdates(locationListener);

我在 GeoLocationHelper class 中添加了对上述位置管理器的权限检查。现在它在 "this" 上返回我所需的上下文。如何从我的 MainActivity 获得许可并将其传递给 GeoLocationHelper class

中的许可检查
void stopRetrievingLocation() {
        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;
        }
        locationManager.removeUpdates(locationListener);

你必须将 Context 传递给你的 GeoLocationHelper class

public class GeoLocationHelper{
    private Context mContext;

    public GeoLocationHelper(Context context){
        mContext = context;
    }

    ----------
}

然后将您的 stopRetrievingLocation 函数更改为:

void stopRetrievingLocation() {
    if (ActivityCompat.checkSelfPermission(mContext, 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;
    }
    locationManager.removeUpdates(locationListener);

在你的 MainActivity

GeoLocationHelper mGeoLocationHelper = new GeoLocationHelper(this);