call_log 2.2.0 call_log: ^2.2.0 copied to clipboard
A Flutter plugin to access and query call history log. Support for Android only as iOS does not provide API for accessing call history.
import 'package:call_log/call_log.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:workmanager/workmanager.dart';
///TOP-LEVEL FUNCTION PROVIDED FOR WORK MANAGER AS CALLBACK
void callbackDispatcher() {
Workmanager.executeTask((task, inputData) async {
print('Background Services are Working!');
try {
Iterable<CallLogEntry> cLog = await CallLog.get();
print('Queried call log entries');
for (CallLogEntry entry in cLog) {
print('-------------------------------------');
print('F. NUMBER : ${entry.formattedNumber}');
print('C.M. NUMBER: ${entry.cachedMatchedNumber}');
print('NUMBER : ${entry.number}');
print('NAME : ${entry.name}');
print('TYPE : ${entry.callType}');
print('DATE : ${DateTime.fromMillisecondsSinceEpoch(entry.timestamp)}');
print('DURATION : ${entry.duration}');
print('ACCOUNT ID : ${entry.phoneAccountId}');
print('SIM NAME : ${entry.simDisplayName}');
print('-------------------------------------');
}
return true;
} on PlatformException catch (e, s) {
print(e);
print(s);
return true;
}
});
}
void main() {
runApp(MyApp());
Workmanager.initialize(callbackDispatcher, isInDebugMode: true);
}
/// example widget for call log plugin
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
Iterable<CallLogEntry> _callLogEntries = <CallLogEntry>[];
@override
Widget build(BuildContext context) {
const TextStyle mono = TextStyle(fontFamily: 'monospace');
final List<Widget> children = <Widget>[];
for (CallLogEntry entry in _callLogEntries) {
children.add(
Column(
children: <Widget>[
const Divider(),
Text('F. NUMBER : ${entry.formattedNumber}', style: mono),
Text('C.M. NUMBER: ${entry.cachedMatchedNumber}', style: mono),
Text('NUMBER : ${entry.number}', style: mono),
Text('NAME : ${entry.name}', style: mono),
Text('TYPE : ${entry.callType}', style: mono),
Text('DATE : ${DateTime.fromMillisecondsSinceEpoch(entry.timestamp)}',
style: mono),
Text('DURATION : ${entry.duration}', style: mono),
Text('ACCOUNT ID : ${entry.phoneAccountId}', style: mono),
Text('SIM NAME : ${entry.simDisplayName}', style: mono),
],
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.start,
),
);
}
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('call_log example')),
body: SingleChildScrollView(
child: Column(
children: <Widget>[
Center(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: RaisedButton(
onPressed: () async {
final Iterable<CallLogEntry> result = await CallLog.query();
setState(() {
_callLogEntries = result;
});
},
child: const Text('Get all'),
),
),
),
Center(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: RaisedButton(
onPressed: () {
Workmanager.registerOneOffTask(
DateTime.now().millisecondsSinceEpoch.toString(),
'simpleTask',
existingWorkPolicy: ExistingWorkPolicy.replace,
);
},
child: const Text('Get all in background'),
),
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Column(children: children),
),
],
),
),
),
);
}
}