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.

115 lines
3.7 KiB

2 years ago
import 'dart:async';
2 years ago
import 'dart:ui';
2 years ago
2 years ago
import 'package:call_log/call_log.dart';
import 'package:flutter/cupertino.dart';
2 years ago
import 'package:flutter/material.dart';
2 years ago
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.getInstance();
2 years ago
final service = FlutterBackgroundService();
await service.configure(
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
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,
),
2 years ago
);
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
service.on("stopService").listen((event) {
service.stopSelf();
});
//定义
2 years ago
int flag = 0;
2 years ago
String? phoneNum = "";
String callState;
Timer.periodic(const Duration(seconds: 1), (timer) async {
2 years ago
//数据储存读取
2 years ago
final SharedPreferences prefs = await SharedPreferences.getInstance();
2 years ago
//获取来电状态
2 years ago
CallState state = await Telephony.instance.callState;
callState = state.name;
2 years ago
//发送内容信息
String content = prefs.getString("refSms")!;
//发送信息的状态
2 years ago
int? numberSet = prefs.getInt("numIndex") ?? 0;
2 years ago
print("号码设置$numberSet 发送内容$content");
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
// print(phoneNum);
switch (numberSet) {
case 0:
2 years ago
print("所有都发");
// Telephony.backgroundInstance
// .sendSms(to: phoneNum!, message: content);
flag = 0;
break;
case 1:
2 years ago
if (flag > 0) {
print("来电拒接");
// print("来电拒接/未接");
// print("${phoneNum!}:${ref!}");
// Telephony.backgroundInstance
// .sendSms(to: phoneNum!, message: content);
// print("发送成功");
} else if (flag == -1) {
print("来电接听");
// print("来电接听");
// print("${phoneNum!}:$content");
// Telephony.backgroundInstance
// .sendSms(to: phoneNum!, message: content);
2 years ago
}
2 years ago
flag = 0;
break;
case 2:
2 years ago
if (entry.first.duration! > 0) {
print("去电接听");
// print("${phoneNum!}:$content");
// Telephony.backgroundInstance
// .sendSms(to: phoneNum!, message: content);
} else {
print("去电挂断");
// print("${phoneNum!}:$content");
// Telephony.backgroundInstance
// .sendSms(to: phoneNum!, message: content);
2 years ago
}
2 years ago
flag = 0;
break;
2 years ago
}
2 years ago
}
2 years ago
} else if (callState == "RINGING") {
flag = 1;
2 years ago
print("打印");
2 years ago
} else if (callState == "OFFHOOK") {
if (flag > 0) flag *= -1;
if (flag == 0) flag = -2;
2 years ago
print("打印");
2 years ago
}
2 years ago
});
}