是否已激活GPS - 颤动

Gre*_*Eye 4 dart flutter

如果GPS被激活或停用,有没有办法找到Flutter?我使用插件位置但是我只获得位置而不是GPS的状态.

Cop*_*oad 15

接受的答案使用过时的插件,你可以使用Geolocator插件,

bool isLocationEnabled = await Geolocator().isLocationServiceEnabled();
Run Code Online (Sandbox Code Playgroud)

  • 谢谢,但是您如何显示激活对话框? (3认同)

hum*_*zed 10

先前的答案已过时。

使用最新版本的Geolocator 5.0

var isGpsEnabled = await Geolocator().isLocationServiceEnabled();
Run Code Online (Sandbox Code Playgroud)

如果禁用,我将使用此方法检查并启用Gps。

  Future _checkGps() async {
    if (!(await Geolocator().isLocationServiceEnabled())) {
      if (Theme.of(context).platform == TargetPlatform.android) {
        showDialog(
          context: context,
          builder: (BuildContext context) {
            return AlertDialog(
              title: Text("Can't get gurrent location"),
              content:
                  const Text('Please make sure you enable GPS and try again'),
              actions: <Widget>[
                FlatButton(
                  child: Text('Ok'),
                  onPressed: () {
                    final AndroidIntent intent = AndroidIntent(
                        action: 'android.settings.LOCATION_SOURCE_SETTINGS');

                    intent.launch();
                    Navigator.of(context, rootNavigator: true).pop();
                  },
                ),
              ],
            );
          },
        );
      }
    }
  }
Run Code Online (Sandbox Code Playgroud)

  • 非常感谢。这个评论救了我。那些想知道的人,AndroidIntent()来自名为android_intent的包。 (2认同)

Bos*_*rot 6

通过地理定位,您可以检查位置服务是否正常运行.它通常包括比定位包更多的可定制性.

bool serviceStatus = await _locationService.serviceEnabled();
if (service) {
    // service enabled
} else {
    // service not enabled, restricted or permission denied
}
Run Code Online (Sandbox Code Playgroud)