Flutter后台运行时调用network_info_plus获取WiFi信息返回null
问题:Flutter应用后台运行时获取WiFi名称返回null,前台正常
我已添加获取WiFi名称及实现后台运行的相关依赖,应用后台运行状态正常,但在后台调用获取WiFi名称的接口时返回null,前台运行时则能正常获取到WiFi名称。
相关代码如下:
import 'package:awesome_notifications/awesome_notifications.dart'; import 'package:connectivity_plus/connectivity_plus.dart'; import 'package:flutter/material.dart'; import 'package:permission_handler/permission_handler.dart'; import 'package:network_info_plus/network_info_plus.dart'; import 'package:flutter_background_service/flutter_background_service.dart'; import 'package:flutter_local_notifications/flutter_local_notifications.dart '; Future<void> initializeService() async { await flutterLocalNotificationsPlugin.initialize(const InitializationSettings( android: AndroidInitializationSettings('ic_launcher'))); var service = FlutterBackgroundService(); if (Platform.isIOS) { await flutterLocalNotificationsPlugin.initialize( const InitializationSettings(iOS: DarwinInitializationSettings())); } if (Platform.isAndroid) { final androidInfo = await deviceInfo.getWifiName(); log("androidInfo=======================>$androidInfo"); } await flutterLocalNotificationsPlugin .resolvePlatformSpecificImplementation< AndroidFlutterLocalNotificationsPlugin>() ?.createNotificationChannel(notificationChannel); await service.configure( iosConfiguration: IosConfiguration(onBackground: iosBackground, onForeground: onStart), androidConfiguration: AndroidConfiguration( onStart: onStart, autoStart: true, notificationChannelId: "Foreground Service", isForegroundMode: true, initialNotificationTitle: "Foreground Service Running0..", foregroundServiceNotificationId: 90)); service.startService(); } @pragma("vm:entry-point") void onStart(ServiceInstance service) { service.on("setAsForeground").listen((event) { log("Foreground=======================>"); }); service.on("setAsBackground").listen((event) { log("Background=======================>"); }); service.on("stopService").listen((event) { service.stopSelf(); }); Timer.periodic(const Duration(seconds: 4), (timer) async { final info = NetworkInfo(); // var storedWIFI = '"AndroidWifi"'; String storedWIFI = '"JioFiber_5G"'; Connectivity() .onConnectivityChanged .listen((ConnectivityResult result) async { log("onConnectivityChanged===00001===============>$result"); if (result == ConnectivityResult.wifi) { var wifiName = await info.getWifiName(); log("wifiName=====B4 condition===========$storedWIFI=========>$wifiName"); if (storedWIFI == wifiName) { AwesomeNotifications().createNotification( content: NotificationContent( id: 10, channelKey: 'general_notification', title: 'You are connected to $wifiName', body: 'You have checked in.', notificationLayout: NotificationLayout.BigText, ), ); } } }); flutterLocalNotificationsPlugin.show( 90, "Cool Service", "Awesom ${DateTime.now()}", const NotificationDetails( android: AndroidNotificationDetails( "Foreground Service", "Foreground Service Running0..", ongoing: true))); }); }
解决方案
只需在AndroidManifest.xml文件中添加以下权限即可解决:
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
内容的提问来源于stack exchange,提问作者Pradip Ninawe
相关产品推荐
相关产品推荐

