【问题标题】:Flutter: Run function after GPS is enabledFlutter:启用 GPS 后的运行功能
【发布时间】:2020-10-11 14:25:11
【问题描述】:

一旦用户启用 GPS/定位服务,我想运行下面写的 deviceInfo 函数。目前它仅在检查位置服务是否启用时运行。如果它被禁用,那么它会显示警报对话框。

如您所见,我使用的是 If 条件。如果位置被禁用,则显示警报对话框,否则继续下一步。

问题是如果用户启用位置服务,那么我如何执行在 else 条件下编写的代码。

这里是代码。

  Future<void> deviceInfo() async {
      DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
      AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
      print(androidInfo.androidId);
      print('Is Physical Device: ${androidInfo.isPhysicalDevice}');
      physicaldevice = androidInfo.isPhysicalDevice;
      if(physicaldevice == true){
       
        var isGpsEnabled = await Geolocator().isLocationServiceEnabled();

         if(isGpsEnabled == false){
          setState(() {
              visible = false;
          });
          _showAlert(context);
          print(isGpsEnabled);
         

        }else{
         ------ Some Codes if GPS is enabled ------
    }

这是用于 GPS 启用消息的 AlertBox

   void _showAlert(BuildContext context) {
      showDialog(
          context: context,
          builder: (context) => AlertDialog(
            title: Text("Error"),
            content: Text("Location Service is disabled. Please enable it."),
             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();
              })],
          )
      );
    }

简单来说,我想在启用 GPS 后重新运行 DeviceInfo 功能。

【问题讨论】:

    标签: flutter


    【解决方案1】:

    如果 isGpsEnabled 为 false,您可以尝试使用 Timer.periodic

        Future<void> deviceInfo() async {
      DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
      AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
      print(androidInfo.androidId);
      print('Is Physical Device: ${androidInfo.isPhysicalDevice}');
      physicaldevice = androidInfo.isPhysicalDevice;
      if(physicaldevice == true){
       
        var isGpsEnabled = await Geolocator().isLocationServiceEnabled();
    
         if(isGpsEnabled == false){
          setState(() {
              visible = false;
          });
          _showAlert(context);
          print(isGpsEnabled);
           // new code //
           Timer.periodic(Duration(seconds: 1), (timer) async {
           var isGpsEnabled = await Geolocator().isLocationServiceEnabled();
           if (isGpsEnabled) {
           timer.cancel();
            ------ Some Codes if GPS is enabled ------
           }
    });
      
         
    
        }else{
         ------ Some Codes if GPS is enabled ------
    }
    

    【讨论】:

    • 你的解决方案奏效了。我只面临一个问题。如果用户没有启用该位置并返回,则警报框不再可见,因为用户已经点击了“确定”。
    • 我设法让它工作。 Navigator.of(context, rootNavigator: true).pop(_showAlert);在 Time.Periodic 中添加了此代码。非常感谢您的帮助。
    猜你喜欢
    • 2021-04-14
    • 2019-01-29
    • 1970-01-01
    • 1970-01-01
    • 2020-05-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-11
    • 1970-01-01
    相关资源
    最近更新 更多