简体   繁体   English

GPS 在 android 工作室中的状态 - API 级别 29

[英]GPS status in android studio - API level 29

I'm writing an android app with android studio in java targetting API 29 devices.我正在 java 中使用 android 工作室编写 android 应用程序,目标是 ZDB974238714CA8DE634A7CE1D03 设备。 I've written a GPSReceiver (which extends BroadcastReceiver) and I need to know if GPS status has changed (ie add a location listener).我写了一个 GPSReceiver(它扩展了 BroadcastReceiver),我需要知道 GPS 的状态是否发生了变化(即添加一个位置监听器)。

In earlier versions of Android, I know this could be accomplished with在 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;

And in Android 11 (API level 30) the next snippet seems to do it for me.在 Android 11(API 级别 30)中,下一个片段似乎对我有用。

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

if (state)
  do_smth;
else
  do_smth_else;

However, I don't know how get this exact behaviour in API 29.但是,我不知道如何在 API 29 中获得这种确切的行为。

You can try with this code, it works for me您可以尝试使用此代码,它适用于我

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM