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.

118 lines
3.9 KiB

2 years ago
import 'dart:async';
2 years ago
import 'dart:ui';
2 years ago
import 'package:call_log/call_log.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_background_service/flutter_background_service.dart';
2 years ago
import 'package:shared_preferences/shared_preferences.dart';
2 years ago
import 'package:telephony/telephony.dart';
2 years ago
2 years ago
Future<void> initializeService() async {
2 years ago
// SharedPreferences preferences = await SharedPreferences.getInstance();
2 years ago
2 years ago
final service = FlutterBackgroundService();
2 years ago
2 years ago
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: true,
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,
),
);
2 years ago
service.startService();
2 years ago
}
2 years ago
// }
bool onIosBackground(ServiceInstance service) {
2 years ago
WidgetsFlutterBinding.ensureInitialized();
2 years ago
print('FLUTTER BACKGROUND FETCH');
return true;
2 years ago
}
2 years ago
void onStart(ServiceInstance service) async {
2 years ago
DartPluginRegistrant.ensureInitialized();
2 years ago
int flag = 0;
2 years ago
String? phoneNum = "";
int? callRecords = 0;
String callState;
2 years ago
service.on('stopService').listen((event) {
service.stopSelf();
});
2 years ago
// setForegroundNotificationInfo(title: "你好", content: "再见");
// AndroidServiceInstance.
// service.setNotificationInfo("").listen((event){
//
// });
// Telephony.backgroundInstance.sendSms(to: "13486828191", message: "123123");
2 years ago
Timer.periodic(const Duration(seconds: 1), (timer) async {
final SharedPreferences prefs = await SharedPreferences.getInstance();
2 years ago
CallState state = await Telephony.instance.callState;
callState = state.name;
2 years ago
// print("$callState$flag");
2 years ago
// print(entry.first.duration);
2 years ago
String? ref = prefs.getString('refSms');
String? con = prefs.getString('conSms');
String? call = prefs.getString('callSms');
String? idle = prefs.getString('idleSms');
2 years ago
bool? callSw = prefs.getBool('callSwitch');
bool? idleSw = prefs.getBool('idleSwitch');
2 years ago
int? numberSet=prefs.getInt("numIndex");
List<String>? numberList=prefs.getStringList("addressList");
print("号码设置$numberSet 通讯列表$numberList");
2 years ago
if (callState == "IDLE") {
2 years ago
if (flag != 0) {
2 years ago
final Iterable<CallLogEntry> entry = await CallLog.query();
2 years ago
phoneNum = entry.first.number;
2 years ago
callRecords = entry.first.duration;
2 years ago
print(phoneNum);
2 years ago
if (flag > 0) {
print("来电拒接/未接");
2 years ago
print("${phoneNum!}:${ref!}");
Telephony.backgroundInstance.sendSms(to: phoneNum!, message: ref);
2 years ago
print("发送成功");
} else if(flag==-1){
print("来电接听");
2 years ago
print("${phoneNum!}:${con!}");
Telephony.backgroundInstance.sendSms(to: phoneNum!, message: con);
2 years ago
}
else {
if(entry.first.duration!>0){
if(callSw!){
print(callSw);
print("${phoneNum!}:${call!}");
Telephony.backgroundInstance.sendSms(to: phoneNum!, message: call);
2 years ago
}
print("去电接听");
}else{
if(idleSw!){
print(idleSw);
2 years ago
print("${phoneNum!}:${idle!}");
Telephony.backgroundInstance.sendSms(to: phoneNum!, message: idle);
2 years ago
}
print("去电未接");
}
print("发送成功");
2 years ago
}
flag = 0;
2 years ago
}
2 years ago
} else if (callState == "RINGING") {
//响铃
flag=1;
2 years ago
print('通话');
2 years ago
} else if (callState == "OFFHOOK") {
//通话
2 years ago
if (flag > 0) flag *= -1;
if(flag==0)flag=-2;
2 years ago
print('不通话');
2 years ago
}
2 years ago
});
2 years ago
}