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.

225 lines
6.9 KiB

2 years ago
import 'dart:async';
2 years ago
import 'package:bot_toast/bot_toast.dart';
2 years ago
import 'package:call_log/call_log.dart';
2 years ago
import 'package:flutter/material.dart';
2 years ago
import 'package:flutter/services.dart';
import 'package:flutter_background_service/flutter_background_service.dart';
2 years ago
import 'package:flutter_screenutil/flutter_screenutil.dart';
2 years ago
import 'package:flutter_sms/flutter_sms.dart';
2 years ago
import 'package:fluwx/fluwx.dart';
2 years ago
import 'package:get/get_navigation/src/root/get_material_app.dart';
2 years ago
import 'package:power_logger/power_logger.dart';
2 years ago
import 'package:fluwx/fluwx.dart' as fluwx;
2 years ago
2 years ago
import 'package:project_telephony/providers/user_provider.dart';
2 years ago
import 'package:project_telephony/ui/tab_navigator.dart';
2 years ago
import 'package:provider/provider.dart';
2 years ago
import 'package:telephony/telephony.dart';
import 'package:project_telephony/utils/hive_store.dart';
2 years ago
2 years ago
void _sendSMS(String message, List<String> recipients) async {
try{
String result =
await sendSMS(message: message, recipients: recipients, sendDirect: true);
print(result);
} on PlatformException catch(e){
print(e.toString());
}
2 years ago
}
2 years ago
void main() async {
WidgetsFlutterBinding.ensureInitialized();
2 years ago
FlutterError.onError = (details) {
LoggerData.addData(details);
FlutterError.presentError(details);
};
await initializeService();
WidgetsFlutterBinding.ensureInitialized();
2 years ago
runApp(const MyApp());
2 years ago
}
2 years ago
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();
2 years ago
// print('FLUTTER BACKGROUND FETCH');
}
2 years ago
void onStart() {
int flag = 0;
2 years ago
String phoneNum;
String callState;
2 years ago
WidgetsFlutterBinding.ensureInitialized();
Timer.periodic(const Duration(seconds: 1), (timer) async {
CallState state = await Telephony.instance.callState;
callState = state.name;
2 years ago
// print(callState!+" $flag");
2 years ago
if (callState == "IDLE") {
if (flag != 0) {
flag = 0;
// print("object");
final Iterable<CallLogEntry> result = await CallLog.query();
// print(phoneNum);
// String message = "This is a test message!";
// List<String> recipents = ["10000", "10086"];
// String _result = await sendSMS(message: message, recipients: recipents, sendDirect: true)
// .catchError((onError) {
// print(onError);
// });
// print(_result);
2 years ago
phoneNum = result.first.number!;
// List<String> recipents=[
// phoneNum
// ];
if(phoneNum.isEmpty){
print("At Least 1 Person or Message Required");
}else{
print("你好"+phoneNum);
_sendSMS("你好", [phoneNum]);
print("你好"+phoneNum);
}
// final SmsSendStatusListener listener = (SendStatus status) {
// print(status);
// };
// Phone.telephony.sendSms(
// to: phoneNum!,
// message: "hello",
// statusListener: listener,
// isMultipart: true,
//
// );
2 years ago
}
} else if (callState == "RINGING") {
flag++;
} else if (callState == "OFFHOOK") {
flag++;
}
});
}
class Phone {
static Telephony telephony = Telephony.instance;
}
2 years ago
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String _message = "";
2 years ago
// This will not work as the instance will be replaced by
// the one in background.
final telephony = Telephony.instance;
2 years ago
@override
void initState() {
super.initState();
2 years ago
final service = FlutterBackgroundService();
2 years ago
Future.delayed(const Duration(milliseconds: 0), () async {
//Hive.initFlutter;
await HiveStore.init();
2 years ago
registerWxApi(
appId: "wxd930ea5d5a228f5f",universalLink:"https://your.univerallink.com/link/ "
);
2 years ago
});
2 years ago
final inbox = Telephony.instance.getInboxSms();
2 years ago
// JPush jPush=JPush();
// jPush.setup(
// appKey: "",
// channel: "theChannel",
// production: false,
// debug: true
// );
2 years ago
// service.start();
// List<Permission> permissions = [
// Permission.sms,
// Permission.phone,
// ];
// PermissionHelper.check(permissions, onSuccess: () {
// print('onSuccess');
// }, onFailed: () {
// print('onFailed');
// }, onOpenSetting: () {
// print('onOpenSetting');
// openAppSettings();
// });
2 years ago
}
2 years ago
// 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;
// }
2 years ago
@override
Widget build(BuildContext context) {
2 years ago
return MultiProvider(
providers: [
ChangeNotifierProvider(create: (context) => UserProvider()),
],
child: MediaQuery(
2 years ago
data: MediaQueryData.fromWindow(WidgetsBinding.instance.window),
2 years ago
child: ScreenUtilInit(
designSize: const Size(750, 1334),
builder: (context, child) {
return AnnotatedRegion<SystemUiOverlayStyle>(
value: const SystemUiOverlayStyle(
statusBarColor: Colors.transparent, //状态栏背景色
statusBarIconBrightness: Brightness.dark),
child: GetMaterialApp(
// get.testmode=true,
debugShowCheckedModeBanner: false,
home: const TabNavigator(),
builder: (context, child) {
// ScreenUtil.setContext(context);
return MediaQuery(
//设置文字大小不随系统设置改变
data: MediaQueryData.fromWindow(
WidgetsBinding.instance!.window)
.copyWith(textScaleFactor: 1.0),
child: BotToastInit().call(context, child),
);
},
navigatorObservers: [BotToastNavigatorObserver()],
));
},
)),
);
2 years ago
}
}