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.
183 lines
6.3 KiB
183 lines
6.3 KiB
import 'dart:async';
|
|
import 'dart:ui';
|
|
|
|
import 'package:call_log/call_log.dart';
|
|
import 'package:flutter/cupertino.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_background_service/flutter_background_service.dart';
|
|
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
import 'package:telephony/telephony.dart';
|
|
|
|
const nId = "my_foreground";
|
|
const notificationId = 888;
|
|
Future<void> initializeService() async {
|
|
final service = FlutterBackgroundService();
|
|
const AndroidNotificationChannel channel = AndroidNotificationChannel(
|
|
nId, // id
|
|
'我的前台服务', // title
|
|
description:
|
|
'此通道用于重要通知。', // description
|
|
importance: Importance.low, // importance must be at low or higher level
|
|
);
|
|
final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
|
|
FlutterLocalNotificationsPlugin();
|
|
await flutterLocalNotificationsPlugin
|
|
.resolvePlatformSpecificImplementation<
|
|
AndroidFlutterLocalNotificationsPlugin>()
|
|
?.createNotificationChannel(channel);
|
|
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,
|
|
),
|
|
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,
|
|
|
|
notificationChannelId: nId, // this must match with notification channel you created above.
|
|
initialNotificationTitle: 'AWESOME SERVICE',
|
|
initialNotificationContent: 'Initializing',
|
|
foregroundServiceNotificationId: notificationId,
|
|
),
|
|
);
|
|
// service.startService();
|
|
}
|
|
|
|
bool onIosBackground(ServiceInstance service) {
|
|
WidgetsFlutterBinding.ensureInitialized();
|
|
print('FLUTTER BACKGROUND FETCH');
|
|
return true;
|
|
}
|
|
|
|
// @pragma('vm:entry-point')
|
|
void onStart(ServiceInstance service) async {
|
|
DartPluginRegistrant.ensureInitialized();
|
|
final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
|
|
FlutterLocalNotificationsPlugin();
|
|
service.on("stopService").listen((event) {
|
|
service.stopSelf();
|
|
});
|
|
//定义
|
|
int flag = 0;
|
|
String? phoneNum = "";
|
|
String callState;
|
|
|
|
Timer.periodic(const Duration(seconds: 1), (timer) async {
|
|
//数据储存读取
|
|
final SharedPreferences prefs = await SharedPreferences.getInstance();
|
|
//获取来电状态
|
|
CallState state = await Telephony.instance.callState;
|
|
callState = state.name;
|
|
List<String> noNumberList = prefs.getStringList("specified") ??[];
|
|
flutterLocalNotificationsPlugin.show(
|
|
notificationId,
|
|
'短信帮手',
|
|
'$callState ${DateTime.now().second}',
|
|
const NotificationDetails(
|
|
android: AndroidNotificationDetails(
|
|
nId,
|
|
'我的前台服务',
|
|
icon: 'ic_bg_service_small',
|
|
ongoing: true,
|
|
),
|
|
),
|
|
);
|
|
|
|
//发送内容信息
|
|
String content = prefs.getString("refSms") ?? "现在有事,等会回电";
|
|
//发送信息的状态
|
|
int? numberSet = prefs.getInt("numIndex") ?? 1;
|
|
print("号码设置$numberSet 发送内容$content 指定不发送$noNumberList");
|
|
// print(flag);
|
|
if (callState == "IDLE") {
|
|
if (flag != 0) {
|
|
final Iterable<CallLogEntry> entry = await CallLog.query();
|
|
phoneNum = entry.first.number;
|
|
// print(phoneNum);
|
|
switch (numberSet) {
|
|
case 0:
|
|
|
|
if(!noNumberList.contains(phoneNum)){
|
|
print("所有都发+号码设置$numberSet+发送内容$content");
|
|
Telephony.backgroundInstance
|
|
.sendSms(to: phoneNum!, message: content);
|
|
flag = 0;
|
|
}else{
|
|
print("指定不发送");
|
|
flag = 0;
|
|
}
|
|
break;
|
|
case 1:
|
|
if (flag > 0) {
|
|
// print("来电拒接/未接");
|
|
// print("${phoneNum!}:${ref!}");
|
|
if(!noNumberList.contains(phoneNum)) {
|
|
print("来电拒接+号码设置$numberSet+发送内容$content");
|
|
Telephony.backgroundInstance
|
|
.sendSms(to: phoneNum!, message: content);
|
|
flag = 0;
|
|
}else{
|
|
print("指定不发送");
|
|
flag = 0;
|
|
}
|
|
// print("发送成功");
|
|
} else if (flag == -1) {
|
|
// print("来电接听");
|
|
// print("${phoneNum!}:$content");
|
|
if(!noNumberList.contains(phoneNum)) {
|
|
print("来电接听+号码设置$numberSet+发送内容$content");
|
|
Telephony.backgroundInstance
|
|
.sendSms(to: phoneNum!, message: content);
|
|
flag = 0;
|
|
}else{
|
|
print("指定不发送");
|
|
flag = 0;
|
|
}
|
|
}
|
|
|
|
break;
|
|
case 2:
|
|
if(flag==-2){
|
|
if (entry.first.duration! > 0) {
|
|
// print("${phoneNum!}:$content");
|
|
if(!noNumberList.contains(phoneNum)) {
|
|
print("去电接听+号码设置$numberSet+发送内容$content");
|
|
Telephony.backgroundInstance
|
|
.sendSms(to: phoneNum!, message: content);
|
|
}
|
|
} else {
|
|
// print("${phoneNum!}:$content");
|
|
if(!noNumberList.contains(phoneNum)) {
|
|
print("去电挂断+号码设置$numberSet+发送内容$content");
|
|
Telephony.backgroundInstance
|
|
.sendSms(to: phoneNum!, message: content);
|
|
}else{
|
|
print("指定不发送");
|
|
flag = 0;
|
|
}
|
|
}
|
|
}
|
|
flag = 0;
|
|
break;
|
|
}
|
|
}
|
|
} else if (callState == "RINGING") {
|
|
flag = 1;
|
|
print("打印");
|
|
} else if (callState == "OFFHOOK") {
|
|
if (flag > 0) flag *= -1;
|
|
if (flag == 0) flag = -2;
|
|
print("打印");
|
|
}
|
|
});
|
|
}
|