background_service 1.1.0 background_service: ^1.1.0 copied to clipboard
background service support for ios and Android, support bluetooth service in background
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:background_service/background_service.dart';
void main() {
WidgetsFlutterBinding.ensureInitialized();
BackgroundService.initialize(onStart);
runApp(MyApp());
}
void onStart() {
WidgetsFlutterBinding.ensureInitialized();
final service = BackgroundService();
service.onDataReceived.listen((event) {
if (event!["action"] == "setAsForeground") {
service.setForegroundMode(true);
return;
}
if (event["action"] == "setAsBackground") {
service.setForegroundMode(false);
}
if (event["action"] == "stopService") {
service.stopBackgroundService();
}
});
// bring to foreground
service.setForegroundMode(true);
Timer.periodic(Duration(seconds: 1), (timer) async {
if (!(await service.isServiceRunning())) timer.cancel();
service.setNotificationInfo(
title: "My App Service",
content: "Updated at ${DateTime.now()}",
);
service.sendData(
action: DateTime.now().toIso8601String(),
);
});
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String text = "Stop Service";
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Service App'),
),
body: Column(
children: [
StreamBuilder<Map<String, dynamic>?>(
stream: BackgroundService().onDataReceived,
builder: (context, snapshot) {
if (!snapshot.hasData) {
return Center(
child: CircularProgressIndicator(),
);
}
final data = snapshot.data!;
DateTime? date = DateTime.tryParse(data["current_date"]);
return Text(date.toString());
},
),
ElevatedButton(
child: Text("Foreground Mode"),
onPressed: () {
BackgroundService().sendData(action: "setAsForeground");
},
),
ElevatedButton(
child: Text("Background Mode"),
onPressed: () {
BackgroundService().sendData(action: "setAsBackground");
},
),
ElevatedButton(
child: Text(text),
onPressed: () async {
var isRunning = await BackgroundService().isServiceRunning();
if (isRunning) {
BackgroundService().sendData(
action: "stopService",
);
} else {
BackgroundService.initialize(onStart);
}
if (!isRunning) {
text = 'Stop Service';
} else {
text = 'Start Service';
}
setState(() {});
},
),
],
),
floatingActionButton: FloatingActionButton(
onPressed: () {
BackgroundService().sendData(action: "world");
},
child: Icon(Icons.play_arrow),
),
),
);
}
}