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.

94 lines
2.5 KiB

2 years ago
2 years ago
import 'dart:async';
2 years ago
import 'dart:io';
import 'dart:ui';
2 years ago
import 'package:call_log/call_log.dart';
import 'package:flutter/cupertino.dart';
2 years ago
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
2 years ago
import 'package:flutter_background_service/flutter_background_service.dart';
import 'package:flutter_sms/flutter_sms.dart';
2 years ago
import 'package:shared_preferences/shared_preferences.dart';
2 years ago
2 years ago
import 'package:telephony/telephony.dart';
2 years ago
2 years ago
import './call_sms.dart';
2 years ago
Future<void> _sendSMS(String message, List<String> recipients) async {
2 years ago
try{
String result =
await sendSMS(message: message, recipients: recipients, sendDirect: true);
print(result);
} catch (error){
print(error.toString());
}
2 years ago
}
2 years ago
2 years ago
Future<void> initializeService() async {
2 years ago
SharedPreferences preferences = await SharedPreferences.getInstance();
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: 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) {
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 {
DartPluginRegistrant.ensureInitialized();
2 years ago
int flag = 0;
String phoneNum="";
String callState;
2 years ago
Timer.periodic(const Duration(seconds: 1), (timer) async {
2 years ago
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> result = await CallLog.query();
2 years ago
phoneNum = result.first.number!;
2 years ago
if(phoneNum.isEmpty){
print("At Least 1 Person or Message Required");
}else{
2 years ago
print("你好$phoneNum");
// print("你好123123$phoneNum");
2 years ago
}
}
} else if (callState == "RINGING") {
flag++;
} else if (callState == "OFFHOOK") {
flag++;
}
2 years ago
});
2 years ago
}