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.

124 lines
4.1 KiB

2 years ago
2 years ago
import 'dart:async';
2 years ago
import 'dart:ui';
2 years ago
import 'package:call_log/call_log.dart';
2 years ago
2 years ago
import 'package:flutter/cupertino.dart';
import 'package:flutter_background_service/flutter_background_service.dart';
2 years ago
import 'package:get/get.dart';
2 years ago
import 'package:project_telephony/utils/user_tool.dart';
2 years ago
import 'package:project_telephony/utils/hive_store.dart';
2 years ago
import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart';
2 years ago
import 'package:telephony/telephony.dart';
2 years ago
2 years ago
import '../../providers/user_provider.dart';
2 years ago
import '../../utils/user_tool.dart';
2 years ago
2 years ago
2 years ago
Future<void> initializeService() async {
2 years ago
// SharedPreferences preferences = await SharedPreferences.getInstance();
2 years ago
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
2 years ago
autoStart: true,
2 years ago
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) {
2 years ago
WidgetsFlutterBinding.ensureInitialized();
2 years ago
print('FLUTTER BACKGROUND FETCH');
return true;
2 years ago
}
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="";
2 years ago
int? callRecords=0;
2 years ago
String callState;
2 years ago
service.on('stopService').listen((event) {
service.stopSelf();
});
2 years ago
2 years ago
2 years ago
Timer.periodic(const Duration(seconds: 1), (timer) async {
2 years ago
final SharedPreferences prefs = await SharedPreferences.getInstance();
2 years ago
CallState state = await Telephony.instance.callState;
callState = state.name;
2 years ago
String? ref=prefs.getString('refSms');
String? con=prefs.getString('conSms');
print(ref);
2 years ago
// bool? kg= prefs.getBool("kg");
// // bool? kg= prefs.getBool("kg");
print("这是数据${prefs.getBool("kg")}");
// print("这是数据$kg");
// kg = UserTool.userProvider.kg;
// print("这是数据${prefs.getBool("kg")}");
2 years ago
// print(callState!+" $flag");
if (callState == "IDLE") {
2 years ago
if (flag != 0) {
2 years ago
flag = 0;
// print("object");
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(prefs.getString('action'));
2 years ago
// DateTime.fromMillisecondsSinceEpoch(entry.first.timestamp!)
2 years ago
// print('DURATION : ${entry.first.duration}');///通话时长
2 years ago
// if(callRecords!=0){
// // if(kg!){
2 years ago
Telephony.backgroundInstance.sendSms(to: phoneNum!, message: ref!);
2 years ago
// // }
// }else{
// print("没接通");
2 years ago
if (callRecords != 0) {
2 years ago
print("接通了");
2 years ago
Telephony.backgroundInstance.sendSms(to: phoneNum!, message: "接通了");
2 years ago
} else {
2 years ago
print("没接通");
2 years ago
Telephony.backgroundInstance.sendSms(to: phoneNum!, message: "接通了");
2 years ago
// if(kg!) {
// Telephony.backgroundInstance.sendSms(to: phoneNum!, message: "没接通");
// }
2 years ago
// print('DATE : ${DateTime.fromMillisecondsSinceEpoch(entry.first.timestamp!)}');//拨通时间
// print("你好$phoneNum");
// // final inbox = telephony.getInboxSms();
// Telephony.backgroundInstance.sendSms(to: phoneNum!, message: "啦啦啦啦啦");
2 years ago
// telephony.sendSms(to: phoneNum, message: "感谢来电");
2 years ago
// _sendSMS('',[phoneNum]);
2 years ago
// print("你好123123$phoneNum");
2 years ago
// }
}
} else if (callState == "RINGING") {
flag++;
// print('flag $flag');
} else if (callState == "OFFHOOK") {
flag++;
// print('flag $flag');
2 years ago
}
}
2 years ago
});
2 years ago
}