Flutter:如何实时收听权限

时间:2019-03-31 16:28:34

标签: android ios dart permissions flutter

我正在开发一个应用程序,在该应用程序中我想不断地听取位置信息和电池使用权限。

示例场景:

  1. 用户打开应用程序
  2. 授予权限访问权限
  3. 进入设置并撤消权限
  4. 再次打开应用程序
  5. 该应用程序显示小吃栏,通知用户权限已被撤消。

我是初学者,正在使用flutter-permissions-handler,下面的代码显示了我的用法。

    _listenForLocationPermission() {
    Future<PermissionStatus> status = PermissionHandler()
        .checkPermissionStatus(PermissionGroup.locationWhenInUse);
    status.then((PermissionStatus status) {
      setState(() {
        _permissionStatus = status;
        if (_permissionStatus != PermissionStatus.granted) {
          _renderOfflineSnackbar('Offline');
        }
      });
    });
  }

任何有关上述建议的建议。

2 个答案:

答案 0 :(得分:1)

我在同一条船上,发现这行得通

您需要使用WidgetsBindingObserver扩展类

class _AppState extends State<App> with WidgetsBindingObserver {
  PermissionStatus _status;
  ...
  ...

然后将这些方法添加到您的课程中

@override
  void dispose() {
    WidgetsBinding.instance.removeObserver(this);
    super.dispose();
  }

  // check permissions when app is resumed
  // this is when permissions are changed in app settings outside of app
  void didChangeAppLifecycleState(AppLifecycleState state) {
    if (state == AppLifecycleState.resumed) {
      PermissionHandler()
          .checkPermissionStatus(PermissionGroup.locationWhenInUse)
          .then(_updateStatus);
    }
  }

下面是我的完整代码,但为了使内容简短,我没有添加构建小部件

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:permission_handler/permission_handler.dart';

void main() => runApp(App());

class App extends StatefulWidget {
  @override
  _AppState createState() => _AppState();
}

class _AppState extends State<App> with WidgetsBindingObserver {
  PermissionStatus _status;

  // check permissions
  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addObserver(this);
    PermissionHandler() // Check location permission has been granted
        .checkPermissionStatus(PermissionGroup
            .locationWhenInUse) //check permission returns a Future
        .then(_updateStatus); // handling in callback to prevent blocking UI
  }

  @override
  void dispose() {
    WidgetsBinding.instance.removeObserver(this);
    super.dispose();
  }

  // check permissions when app is resumed
  // this is when permissions are changed in app settings outside of app
  void didChangeAppLifecycleState(AppLifecycleState state) {
    if (state == AppLifecycleState.resumed) {
      PermissionHandler()
          .checkPermissionStatus(PermissionGroup.locationWhenInUse)
          .then(_updateStatus);
    }
  }

override
  Widget build(BuildContext context) {
    return MaterialApp(  
    ...
    ...
}

void _updateStatus(PermissionStatus status) {
    if (status != _status) {
      // check status has changed
      setState(() {
        _status = status; // update
      });
    } else {
      if (status != PermissionStatus.granted) {
        PermissionHandler().requestPermissions(
            [PermissionGroup.locationWhenInUse]).then(_onStatusRequested);
      }
      }
    }
  }

  void _askPermission() {
    PermissionHandler().requestPermissions(
        [PermissionGroup.locationWhenInUse]).then(_onStatusRequested);
  }

  void _onStatusRequested(Map<PermissionGroup, PermissionStatus> statuses) {
    final status = statuses[PermissionGroup.locationWhenInUse];
    if (status != PermissionStatus.granted) {
      // On iOS if "deny" is pressed, open App Settings
      PermissionHandler().openAppSettings();
    } else {
      _updateStatus(status);
    }
  }

我希望这对您有帮助

答案 1 :(得分:0)

空安全码:

permission_handler: ^8.0.0+2

这个想法是在状态恢复时检查应用程序生命周期回调中的权限。这是让您前进的最少代码。

class _FooPageState extends State<FooPage> with WidgetsBindingObserver {
  final Permission _permission = Permission.location;
  bool _checkingPermission = false;

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance!.addObserver(this);
  }

  @override
  void dispose() {
    WidgetsBinding.instance!.removeObserver(this);
    super.dispose();
  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    super.didChangeAppLifecycleState(state);
    if (state == AppLifecycleState.resumed && !_checkingPermission) {
      _checkingPermission = true;
      _checkPermission(_permission).then((_) => _checkingPermission = false);
    }
  }

  Future<void> _checkPermission(Permission permission) async {
    final status = await permission.request();
    if (status == PermissionStatus.granted) {
      print('Permission granted');
    } else if (status == PermissionStatus.denied) {
      print('Permission denied. Show a dialog and again ask for the permission');
    } else if (status == PermissionStatus.permanentlyDenied) {
      print('Take the user to the settings page.');
    }
  }

  @override
  Widget build(BuildContext context) => Scaffold();
}