在 flutter 中以编程方式打开(启用)GPS - android

问题描述 投票:0回答:1

我有 2 个脚本,我尝试启用 GPS 但它们似乎都不起作用,那么我如何为前台-后台任务启用 GPS 几分钟,然后再次关闭它? (不是手动而是通过脚本以编程方式)。

(当然,如果用户给予我在后台运行位置的完全权限)

脚本1:

Future<void> setupBackgroundLocation() async {
  // Set up background location
  await BackgroundLocation.setAndroidNotification(
    title: "Notification title",
    message: "Notification message",
    icon: "@mipmap/ic_launcher",
  );

  await BackgroundLocation.setAndroidConfiguration(1000);
  // Start the location service
  BackgroundLocation.startLocationService(
      forceAndroidLocationManager: true, distanceFilter: 0);
  BackgroundLocation.getLocationUpdates((location) {
    try {
      print(location);
    } catch (e) {
      print("Error getting location updates: $e");
    }
  });

  BackgroundLocation.stopLocationService();
}

脚本2:

import 'dart:async';
import 'package:flutter_background_service/flutter_background_service.dart';
import 'package:location/location.dart';

@pragma('vm:entry-point')
Future<void> onStart(ServiceInstance service) async {

  Timer.periodic(Duration(seconds: 20), (Timer timer) async {

    try {
      Location location = Location();
      bool _serviceEnabled;
      PermissionStatus _permissionGranted;
      LocationData _locationData;

      _serviceEnabled = await location.serviceEnabled();
      if (!_serviceEnabled) {
        _serviceEnabled = await location.requestService();
        if (!_serviceEnabled) {
          return;
        }
      }

      _permissionGranted = await location.hasPermission();
      if (_permissionGranted == PermissionStatus.denied) {
        _permissionGranted = await location.requestPermission();
        if (_permissionGranted != PermissionStatus.granted) {
          return;
        }
      }

      _locationData = await location.getLocation();
      location.onLocationChanged.listen((LocationData currentLocation) {});
      location.enableBackgroundMode(enable: true);
    } catch (e) {
      print('Error $e');
    }
  });
}

我也尝试过地理定位器包,但我找不到任何命令来启用它,如果有帮助的话,这里也是脚本:

import 'dart:async';
import 'package:flutter_background_service/flutter_background_service.dart';
import 'package:geolocator/geolocator.dart';

@pragma('vm:entry-point')
Future<void> onStart(ServiceInstance service) async {
  StreamSubscription<Position>? positionStreamSubscription;
  late LocationSettings locationSettings;
  locationSettings = AndroidSettings(
    accuracy: LocationAccuracy.high,
    forceLocationManager: false,
    intervalDuration: const Duration(milliseconds: 500),
    //distanceFilter: 50,
  );
  Geolocator.getPositionStream(locationSettings: locationSettings);

  Timer.periodic(Duration(seconds: 20), (Timer timer) async {
    positionStreamSubscription?.cancel();

    try {
      positionStreamSubscription = Geolocator.getPositionStream(
              locationSettings:
                  AndroidSettings(intervalDuration: Duration(seconds: 1)))
          .listen((Position position) async {
        double geofenceLatitude = xxxxx;
        double geofenceLongitude = xxxxx;
        double geofenceRadius = 60;

        double distance = await Geolocator.distanceBetween(
          position.latitude,
          position.longitude,
          geofenceLatitude,
          geofenceLongitude,
        );

        if (distance <= geofenceRadius) {
          print('Inside geofence 100');
          // positionStreamSubscription?.cancel();
          // await service.stopSelf();
        } else {
          print('Outside geofence 100');
        }
      });
    } catch (e) {
      print(e);
    }
  });
}

那么我需要一些本机代码吗?或者从 Android 10-14 及更高版本开始,我们只能从弹出窗口获取用户权限??

谢谢。

android flutter flutter-dependencies android-gps
1个回答
0
投票

出于安全原因,您不能。只有用户可以启用或禁用 GPS,因此用户可以保护自己的隐私免受应用程序的侵害。您所能做的就是检查它是否已启用,并要求用户自行将其打开。

© www.soinside.com 2019 - 2024. All rights reserved.