所以我的问题很简单,那就是我不知道如何在此代码中实现checkPermission。我已经阅读了它,但是仍然无法完全绕开它。我的问题之一是,有些例子要我指出一项活动,但那没有用。

我请求位置更新的行抛出SecurityException,并希望我执行checkPermission。

代码示例和/或解释会让我非常感激。

如果我的解释不足,请事先打扰一下,我在那方面很不好!

public class LocationHandler implements LocationListener {
private final Context mContext;

// flag for GPS status
boolean isGPSEnabled = false;

// flag for network status
boolean isNetworkEnabled = false;

boolean canGetLocation = false;

Location location; // location
double latitude; // latitude
double longitude; // longitude

// The minimum distance to change Updates in meters
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters

// The minimum time between updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute

// Declaring a Location Manager
protected LocationManager mLocationManager;

public LocationHandler(Context context) {
    this.mContext = context;
    mLocationManager = (LocationManager) mContext
            .getSystemService(Context.LOCATION_SERVICE);

    //Check Permission
    mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
        MIN_TIME_BW_UPDATES,
        MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
}

@Override
public void onLocationChanged(Location location) {

    String msg = "New Latitude: " + location.getLatitude()
            + "New Longitude: " + location.getLongitude();

    Toast.makeText(mContext, msg, Toast.LENGTH_LONG).show();

}

@Override
public void onProviderDisabled(String provider) {

    Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
    mContext.startActivity(intent);
    Toast.makeText(mContext, "Gps is turned off!! ",
            Toast.LENGTH_SHORT).show();
}

@Override
public void onProviderEnabled(String provider) {

    Toast.makeText(mContext, "Gps is turned on!! ",
            Toast.LENGTH_SHORT).show();
}

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

}
}

最佳答案

这是一些可能有用的代码。

对于安全异常,您将需要使用try / catch包装位置请求。

添加到strings.xml:

<string name="location_permission_rationale">"Location permission is needed for providing nearby services."</string>


导入此:

import static android.Manifest.permission.ACCESS_FINE_LOCATION;


您的活动类和LocationHandler中需要

/**
 * Id to identity LOCATION permission request.
 */
private static final int REQUEST_LOCATION = 0;


...

覆盖您的活动类别:

/**
 * Callback received when a permissions request has been completed.
 */
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                       @NonNull int[] grantResults) {
    if (requestCode == REQUEST_LOCATION) {
        if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            // You can request location here and update your vars
        }
    }
}


将此添加到LocationHandler类,然后从构造函数调用。如果返回true,则表示您是gtg。如果返回false,则必须在回调中进行位置更新。

private boolean requestLocationPermission() {

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
        return true;
    }
    if (checkSelfPermission(mContext, ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
        return true;
    }

    if (shouldShowRequestPermissionRationale((Activity)mContext, ACCESS_FINE_LOCATION)) {

        Snackbar.make(((Activity)mContext).getWindow().getDecorView().findViewById(android.R.id.content),
                ((Activity)mContext).getResources().getString(R.string.location_permission_rationale), Snackbar.LENGTH_INDEFINITE)
                .setAction(android.R.string.ok, new View.OnClickListener() {
                    @Override
                    @TargetApi(Build.VERSION_CODES.M)
                    public void onClick(View v) {
                        requestPermissions((Activity)mContext, new String[]{ACCESS_FINE_LOCATION}, REQUEST_LOCATION);
                    }
                });
    } else {
        requestPermissions((Activity)mContext, new String[]{ACCESS_FINE_LOCATION}, REQUEST_LOCATION);
    }
    return false;
}

09-26 09:02