|
|
|
import 'dart:async';
|
|
|
|
import 'package:call_log/call_log.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter_background_service/flutter_background_service.dart';
|
|
|
|
import 'package:permission_handler/permission_handler.dart';
|
|
|
|
import 'package:project_telephony/permission.dart';
|
|
|
|
import 'package:telephony/telephony.dart';
|
|
|
|
|
|
|
|
onBackgroundMessage(SmsMessage message) {
|
|
|
|
debugPrint("onBackgroundMessage called");
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> main() async {
|
|
|
|
WidgetsFlutterBinding.ensureInitialized();
|
|
|
|
await initializeService();
|
|
|
|
runApp(const MyApp());
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> initializeService() async {
|
|
|
|
final service = FlutterBackgroundService();
|
|
|
|
service.setNotificationInfo(title: '短信助手',content: '正在后台运行');
|
|
|
|
await service.configure(
|
|
|
|
androidConfiguration: AndroidConfiguration(
|
|
|
|
onStart: onStart,
|
|
|
|
autoStart: true,
|
|
|
|
isForegroundMode: true,
|
|
|
|
),
|
|
|
|
iosConfiguration: IosConfiguration(
|
|
|
|
autoStart: true,
|
|
|
|
onForeground: onStart,
|
|
|
|
onBackground: onIosBackground,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
void onIosBackground() {
|
|
|
|
WidgetsFlutterBinding.ensureInitialized();
|
|
|
|
print('FLUTTER BACKGROUND FETCH');
|
|
|
|
}
|
|
|
|
|
|
|
|
void onStart() {
|
|
|
|
int flag = 0;
|
|
|
|
String? phoneNum,callState;
|
|
|
|
WidgetsFlutterBinding.ensureInitialized();
|
|
|
|
|
|
|
|
Timer.periodic(const Duration(seconds: 1), (timer) async {
|
|
|
|
CallState state = await Telephony.instance.callState;
|
|
|
|
callState=state.name;
|
|
|
|
print(callState!+"$flag");
|
|
|
|
if (callState == "IDLE") {
|
|
|
|
if (flag != 0) {
|
|
|
|
flag = 0;
|
|
|
|
final Iterable<CallLogEntry> result = await CallLog.query();
|
|
|
|
phoneNum = result.first.number;
|
|
|
|
print(phoneNum);
|
|
|
|
Phone.telephony.sendSms(to: phoneNum!, message: "hello",isMultipart:true,);
|
|
|
|
}
|
|
|
|
} else if (callState == "RINGING") {
|
|
|
|
flag++;
|
|
|
|
} else if (callState == "OFFHOOK") {
|
|
|
|
flag++;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
class Phone{
|
|
|
|
static Telephony telephony = Telephony.instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
class MyApp extends StatefulWidget {
|
|
|
|
const MyApp({Key? key}) : super(key: key);
|
|
|
|
|
|
|
|
@override
|
|
|
|
_MyAppState createState() => _MyAppState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _MyAppState extends State<MyApp> {
|
|
|
|
String _message = "";
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
final service = FlutterBackgroundService();
|
|
|
|
service.start();
|
|
|
|
List<Permission> permissions = [
|
|
|
|
Permission.sms,
|
|
|
|
Permission.phone,
|
|
|
|
];
|
|
|
|
PermissionHelper.check(permissions,
|
|
|
|
onSuccess: () {
|
|
|
|
print('onSuccess');
|
|
|
|
}, onFailed: () {
|
|
|
|
print('onFailed');
|
|
|
|
}, onOpenSetting: () {
|
|
|
|
print('onOpenSetting');
|
|
|
|
openAppSettings();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
onMessage(SmsMessage message) async {
|
|
|
|
setState(() {
|
|
|
|
_message = message.body ?? "Error reading message body.";
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
onSendStatus(SendStatus status) {
|
|
|
|
setState(() {
|
|
|
|
_message = status == SendStatus.SENT ? "sent" : "delivered";
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> initPlatformState() async {
|
|
|
|
final bool? result = await Phone.telephony.requestPhoneAndSmsPermissions;
|
|
|
|
if (result != null && result) {
|
|
|
|
Phone.telephony.listenIncomingSms(
|
|
|
|
onNewMessage: onMessage, onBackgroundMessage: onBackgroundMessage);
|
|
|
|
}
|
|
|
|
if (!mounted) return;
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return MaterialApp(
|
|
|
|
home: Scaffold(
|
|
|
|
appBar: AppBar(
|
|
|
|
title: const Text('Plugin example app'),
|
|
|
|
),
|
|
|
|
body: Column(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
|
|
children: [
|
|
|
|
TextButton(
|
|
|
|
onPressed: () async {
|
|
|
|
Phone.telephony.sendSms(to: "10086", message: "1");
|
|
|
|
},
|
|
|
|
child: const Text('sms')),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|