当应用不可见(仍在运行)时,我使用background_location: ^0.0.9+3
来获取位置。但是,当我关闭应用程序而不关闭定位服务时,会出现此错误
E / BpSurfaceComposerClient(15446):交易失败(-1) E / ActivityThread(15446):活动 com.example.Location_background.MainActivity已泄漏 服务连接 com.almoullim.background_location.BackgroundLocationPlugin$mServiceConnection$1@5334970 最初绑定在这里E / ActivityThread(15446): android.app.ServiceConnectionLeaked:活动 com.example.Location_background.MainActivity已泄漏 服务连接 com.almoullim.background_location.BackgroundLocationPlugin$mServiceConnection$1@5334970 最初绑定在这里
即使我以处置方式将其关闭也不起作用。
我的代码:
import 'package:background_location/background_location.dart';
import 'package:flutter/material.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Background Location Notifier',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Background Location Notifier'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
void initState() {
super.initState();
}
static void backgroundCallback(location) async {
print('Sample app received data location: $location');
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
new FlutterLocalNotificationsPlugin();
var initializationSettingsAndroid =
AndroidInitializationSettings('@mipmap/ic_launcher');
var initializationSettingsIOS = IOSInitializationSettings();
var initializationSettings = InitializationSettings(
initializationSettingsAndroid, initializationSettingsIOS);
flutterLocalNotificationsPlugin.initialize(initializationSettings);
var androidPlatformChannelSpecifics = new AndroidNotificationDetails(
'location_update',
'Location Updates',
'You will receive location updates here',
importance: Importance.Max,
priority: Priority.High);
var iOSPlatformChannelSpecifics = new IOSNotificationDetails();
var platformChannelSpecifics = new NotificationDetails(
androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
await flutterLocalNotificationsPlugin.show(
0,
'New Location Received !',
"${location.latitude}, ${location.longitude}",
platformChannelSpecifics);
}
void _registerListener() async {
BackgroundLocation.startLocationService();
BackgroundLocation.getLocationUpdates((location) {
backgroundCallback(location);
print(location.toString());
});
}
void _removeListener() async {
BackgroundLocation.stopLocationService();
}
@override
void dispose() {
BackgroundLocation.stopLocationService();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Padding(
padding: EdgeInsets.all(8.0),
child: Column(
children: <Widget>[
RaisedButton(
onPressed: () {
_registerListener();
},
child: const Text('Register background service'),
),
RaisedButton(
onPressed: () {
_removeListener();
},
child: const Text('Remove background service'),
),
],
),
)),
);
}
}