You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

90 lines
3.1 KiB

import 'dart:async';
import 'dart:ui';
import 'package:call_log/call_log.dart';
import 'package:flustars/flustars.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_background_service/flutter_background_service.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:permission_handler_platform_interface/permission_handler_platform_interface.dart';
import 'package:telephony/telephony.dart';
Future<void> initializeService() async {
// SharedPreferences preferences = await SharedPreferences.getInstance();
final service = FlutterBackgroundService();
await service.configure(
androidConfiguration: AndroidConfiguration(
// this will be executed when app is in foreground or background in separated isolate
onStart: onStart,
// auto start service
autoStart: false,
isForegroundMode: true,
),
iosConfiguration: IosConfiguration(
// auto start service
autoStart: true,
// this will be executed when app is in foreground in separated isolate
onForeground: onStart,
// you have to enable background fetch capability on xcode project
onBackground: onIosBackground,
),
);
service.startService();
}
// }
bool onIosBackground(ServiceInstance service) {
WidgetsFlutterBinding.ensureInitialized();
print('FLUTTER BACKGROUND FETCH');
return true;
}
void onStart(ServiceInstance service ) async {
DartPluginRegistrant.ensureInitialized();
int flag = 0;
String? phoneNum="";
int? callRecords=0;
String callState;
Timer.periodic(const Duration(seconds: 1), (timer) async {
CallState state = await Telephony.instance.callState;
callState = state.name;
// print(callState!+" $flag");
if (callState == "IDLE") {
if (flag != 0) {
flag = 0;
// print("object");
final Iterable<CallLogEntry> entry = await CallLog.query();
phoneNum=entry.first.number;
callRecords = entry.first.duration;
// DateTime.fromMillisecondsSinceEpoch(entry.first.timestamp!)
// print('DURATION : ${entry.first.duration}');///通话时长
if(callRecords!=0){
print("接通了");
Telephony.backgroundInstance.sendSms(to: phoneNum!, message: "接通了");
}else{
print("没接通");
Telephony.backgroundInstance.sendSms(to: phoneNum!, message: "没接通");
// print('DATE : ${DateTime.fromMillisecondsSinceEpoch(entry.first.timestamp!)}');//拨通时间
// print("你好$phoneNum");
// // final inbox = telephony.getInboxSms();
// Telephony.backgroundInstance.sendSms(to: phoneNum!, message: "啦啦啦啦啦");
// telephony.sendSms(to: phoneNum, message: "感谢来电");
// _sendSMS('',[phoneNum]);
// print("你好123123$phoneNum");
}
}
} else if (callState == "RINGING") {
flag++;
// print('flag $flag');
} else if (callState == "OFFHOOK") {
flag++;
// print('flag $flag');
}
});
}