【问题标题】:Settings.Secure.LOCATION_PROVIDERS_ALLOWED returns null in Android 12Settings.Secure.LOCATION_PROVIDERS_ALLOWED 在 Android 12 中返回 null
【发布时间】:2022-02-01 11:03:24
【问题描述】:

我创建了一个 android 应用程序,该应用程序在 12 以下的 android 中完美运行。当应用程序在 android 12 中运行时,我遇到了崩溃,错误与以下行有关。 provider 变量为空,我不知道为什么?我已经阅读了有关 android 11+ 中的位置和背景位置的信息,并且我这样做了,但我遇到了这个错误。

public static boolean isGPSEnabled(Context context) {
    LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    String provider = Settings.Secure.getString(KITILApplication.getappContext().getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
    if (provider.contains("gps"))
        return true;
    if (locationManager != null)
        if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
            return true;
        }
    return false;
}

错误是:

 Process: com., PID: 14005
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.contains(java.lang.CharSequence)' on a null object reference
    at com.BaseActivity.isGPSEnabled(BaseActivity.java:78)
    at com.recordLogOnDB(HelperMethods.java:390)
    at com.LoginDialogActivity$3.onResponse(LoginDialogActivity.java:329)
    at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:70)
    at android.os.Handler.handleCallback(Handler.java:938)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loopOnce(Looper.java:201)
    at android.os.Looper.loop(Looper.java:288)
    at android.app.ActivityThread.main(ActivityThread.java:7842)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)

【问题讨论】:

    标签: android location settings


    【解决方案1】:

    LOCATION_PROVIDERS_ALLOWED 此常量在 API 级别 19 中已弃用。此设置从 Android S 开始不再存在,因为它不再能够真实地反映位置设置。请改用 LocationManager.isProviderEnabled(java.lang.String) 或 LocationManager#isLocationEnabled()。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-25
      • 2021-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多