如何在Android 6.0上处理ACCESS_FINE_LOCATION权限?

时间:2016-02-29 11:53:36

标签: android location

当我在使用Android版本6.0或更高版本的设备上启动LocationService时,我会收到以下错误:

  

java.lang.SecurityException:客户端必须具有ACCESS_FINE_LOCATION权限才能请求PRIORITY_HIGH_ACCURACY位置。                                                                           在android.os.Parcel.readException(Parcel.java:1620)                                                                           在android.os.Parcel.readException(Parcel.java:1573)                                                                           在com.google.android.gms.internal.ni $ a $ a.a(未知来源)                                                                           在com.google.android.gms.internal.nj.a(未知来源)                                                                           在com.google.android.gms.internal.nk.a(未知来源)                                                                           在com.google.android.gms.internal.nf $ 1.a(未知来源)                                                                           在com.google.android.gms.internal.nf $ 1.a(未知来源)                                                                           在com.google.android.gms.common.api.BaseImplementation $ a.b(未知来源)                                                                           在com.google.android.gms.common.api.c.a(未知来源)                                                                           在com.google.android.gms.common.api.c.b(未知来源)                                                                           在com.google.android.gms.internal.nf.requestLocationUpdates(未知来源)                                                                           在com.google.android.gms.internal.jm.f(未知来源)                                                                           在com.google.android.gms.common.api.c.gJ(未知来源)                                                                           在com.google.android.gms.common.api.c.d(未知来源)                                                                           在com.google.android.gms.common.api.c $ 2.onConnected(未知来源)                                                                           在com.google.android.gms.internal.jm.f(未知来源)                                                                           在com.google.android.gms.internal.jm.dU(未知来源)                                                                           在com.google.android.gms.internal.jl $ h.b(未知来源)                                                                           在com.google.android.gms.internal.jl $ h.g(未知来源)                                                                           在com.google.android.gms.internal.jl $ b.hy(未知来源)                                                                           在com.google.android.gms.internal.jl $ a.handleMessage(未知来源)                                                                           在android.os.Handler.dispatchMessage(Handler.java:102)                                                                           在android.os.Looper.loop(Looper.java:148)                                                                           在android.app.ActivityThread.main(ActivityThread.java:5466)                                                                           at java.lang.reflect.Method.invoke(Native Method)                                                                           在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726)                                                                           在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

我已在 Manifest.xml 中设置了所需的提交!然后我读到在Android 6.0上提交的内容有所不同,但我如何要求用户接受或设置应用程序所需的预设?

有什么建议吗?

1 个答案:

答案 0 :(得分:2)

首先检查是否已授予权限

if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        requestPermissions(new String[]{Manifest.permission.CAMERA}, 108);
        return;
    } else {
        //continueYourTask
}

如果未被授予,它会自动显示一个授予访问权限的对话框,并在被授予回复时收到

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    switch (requestCode) {
        case 108:
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                //continueYourTask
            }
            break;
        default:
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}