即使我将请求权限放入onCreate中,我也会收到位置权限错误。在我提交请求权限申请后,崩溃后要求获得许可。
我将请求权限放在onCreate中。在我提交请求权限申请后,崩溃后要求获得许可。
private boolean gpsInit() {
mLocService = (LocationManager)
getSystemService(Context.LOCATION_SERVICE);
if (mLocService != null) {
mLocProvider =
mLocService.getProvider(LocationManager.GPS_PROVIDER);
if (mLocProvider != null) {
if (ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION) !=
PackageManager.PERMISSION_GRANTED) {
return true;
}
mLocService.addGpsStatusListener(this);
if
(mLocService.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
gpsStatusTextView.setText(getString(R.string.status_gps_ready));
return true;
}
}
}
gpsStatusTextView.setText
(getString(R.string.status_gps_no_support));
showDialog(NO_GPS_SUPPORT);
Log.e(TAG, "Unable to get GPS PROVIDER");
// todo disable gps controls into Preferences
return false;
}
日志:
java.lang.RuntimeException: Unable to resume activity
{com.quad14.obdnewtry/com.quad14.obdnewtry.activity.MainActivity}: java.lang.SecurityException: "gps" location provider requires ACCESS_FINE_LOCATION permission.
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3429)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3469)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2732)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.SecurityException: "gps" location provider requires ACCESS_FINE_LOCATION permission.
at android.os.Parcel.readException(Parcel.java:1684)
at android.os.Parcel.readException(Parcel.java:1637)
at android.location.ILocationManager$Stub$Proxy.getProviderProperties(ILocationManager.java:1056)
at android.location.LocationManager.getProvider(LocationManager.java:383)
at com.quad14.obdnewtry.activity.MainActivity.gpsInit(MainActivity.java:679)
at com.quad14.obdnewtry.activity.MainActivity.onResume(MainActivity.java:832)
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1269)
at android.app.Activity.performResume(Activity.java:6783)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3406
在Oncreate内部:
if (ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, REQUEST_PERMISSION_LOCATION);
}
} else {
// We have already permission to use the location
}
在此行出现错误
mLocProvider = mLocService.getProvider(LocationManager.GPS_PROVIDER);
我想要的是应用程序应在午餐时征求许可
答案 0 :(得分:1)
确保您在清单中添加权限
如果没有,请在清单文件中写入以下内容。
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>