启用GPS后运行功能

启用GPS后运行功能

本文介绍了Flutter:启用GPS后运行功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想运行deviceInfo函数,该函数在用户启用GPS/定位服务后写在下面.当前,它仅在运行时检查位置服务是否已启用.如果禁用,则会显示警报对话框.

I wanted to run deviceInfo function which is written below once user enabled the GPS/Location Services. Currently it is running only to check Location Services is enabled or not. If its disabled then it shows alert dialog.

您可以看到我正在使用If条件.如果位置被禁用,则显示警报对话框,否则继续下一步.

As you can see i am using If condition. If location is disabled then show the alert dialog else proceed to the next step.

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

Problem is if user enable the locations services then how can i execute code those are written under else condition.

这是代码.

  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

Here is the AlertBox for GPS enable message

   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功能.

In Simple words i want to re-run DeviceInfo function once GPS is enabled.

推荐答案

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

You can try using Timer.periodic if isGpsEnabled is false

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

这篇关于Flutter:启用GPS后运行功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 01:16