【发布时间】:2016-08-03 06:46:52
【问题描述】:
我正在创建一个应用,但我在应用内移动设备的位置访问权限中出现错误。请帮我。使用以下代码......
public static boolean isLocationEnabled(Context context) {
int locationMode = 0;
String locationProviders;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
try {
locationMode = Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.LOCATION_MODE);
} catch (Settings.SettingNotFoundException e) {
e.printStackTrace();
}
return locationMode != Settings.Secure.LOCATION_MODE_OFF;
}else{
locationProviders = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
return !TextUtils.isEmpty(locationProviders);
}
}
我在这方面遇到了错误
"locationMode = Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.LOCATION_MODE);"
请告诉我如何解决它。 谢谢 click here what i want
【问题讨论】:
-
您是否获得了用户的许可?新的 os 6.0 您可能需要在运行时获得许可,请提供您的 logcat。
-
this my logcat" 引起:java.lang.NullPointerException:尝试在 com 的空对象引用上调用虚拟方法 'android.content.ContentResolver android.content.Context.getContentResolver()'。 test.MapsActivity.isLocationEnabled(MapsActivity.java:179)"
-
似乎还不是权限问题。似乎它在抱怨您的上下文。您是否在正确的上下文中通过?你在哪里调用这个函数?在活动或片段中?
-
在 OnCreate "isLocationEnabled(context);"像这样
-
这就是我在活动中调用的方式。 Utils.isLocationEnabled(this);结果成功,如下图所示。