android如何检查为 GeoLocationClass 到 MainActivity 的权限?

标签: Android
发布时间: 2016/9/26 4:05:26
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

权限检查我相处

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);

我已经添加了上述权限检查 location 中的经理我 GeoLocationHelper 类。现在其返回我需要 Context 上"this"。怎样才能从权限我 MainActivity 和它的权限检查中通 GeoLocationHelper

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);

解决方法 1:

你必须通过 Context 向你 GeoLocationHelper

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);
官方微信
官方QQ群
31647020