首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >LocationManager checkPermission

LocationManager checkPermission
EN

Stack Overflow用户
提问于 2017-05-24 13:40:57
回答 2查看 845关注 0票数 0

所以我的问题很简单,我不知道如何将checkPermission实现到这段代码中。我已经读过它了,但我还是不能完全理解它。我的一个问题是,有些例子要求我指出一项活动,但这是行不通的。

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

代码示例和/或解释将使我非常感激。

恕我直言,如果我没有解释的话,那部分我就不擅长了!

代码语言:javascript
复制
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

}
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-05-24 14:11:13

以下是一些可能有帮助的代码。

您将需要将位置请求包装为try/catch,以获得安全异常。

添加到strings.xml中:

代码语言:javascript
复制
<string name="location_permission_rationale">"Location permission is needed for providing nearby services."</string>

进口此:

代码语言:javascript
复制
import static android.Manifest.permission.ACCESS_FINE_LOCATION;

在活动类和LocationHandler中需要

代码语言:javascript
复制
/**
 * Id to identity LOCATION permission request.
 */
private static final int REQUEST_LOCATION = 0; 

..。

在活动类中重写:

代码语言:javascript
复制
/**
 * 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,则必须在回调中执行位置更新。

代码语言:javascript
复制
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;
}
票数 1
EN

Stack Overflow用户

发布于 2017-05-24 13:47:06

您可能忘记在清单中添加这两个权限。

代码语言:javascript
复制
 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
 <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44160127

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档