【问题标题】:GPS status in android studio - API level 29android studio 中的 GPS 状态 - API 级别 29
【发布时间】:2021-07-20 00:53:27
【问题描述】:

我正在使用面向 API 29 设备的 java 中的 android studio 编写一个 android 应用程序。我编写了一个 GPSReceiver(它扩展了 BroadcastReceiver),我需要知道 GPS 状态是否发生了变化(即添加一个位置监听器)。

在早期版本的 Android 中,我知道这可以通过

ContentResolver contentResolver = context.getContentResolver();
int state = Settings.Secure.getInt(contentResolver, Settings.Secure.LOCATION_MODE);
Log.i(TAG, "GPS status changed, new state = " + state);

if (state == Settings.Secure.LOCATION_MODE_OFF)
  do_smth;
else
  do_smth_else;

在 Android 11(API 级别 30)中,下一个 sn-p 似乎可以为我做这件事。

boolean state = intent.getIntExtra(LocationManager.EXTRA_LOCATION_ENABLED, -1);

if (state)
  do_smth;
else
  do_smth_else;

但是,我不知道如何在 API 29 中获得这种确切的行为。

【问题讨论】:

    标签: java android android-studio android-gps


    【解决方案1】:

    你可以试试这个代码,它对我有用

    public boolean isGPSOn() {
        boolean check = false;
        LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        if (locationManager != null) {
            check = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        }
        return check;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-09
      • 2013-10-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多