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.
142 lines
3.6 KiB
142 lines
3.6 KiB
2 years ago
|
import 'package:flutter/material.dart';
|
||
|
import 'package:provider/provider.dart';
|
||
|
import 'package:telephony/telephony.dart';
|
||
|
import 'mainProvider.dart';
|
||
|
|
||
|
onBackgroundMessage(SmsMessage message) {
|
||
|
debugPrint("onBackgroundMessage called");
|
||
|
}
|
||
|
// void backgroundFetchHeadlessTask(HeadlessTask task) async {
|
||
|
// String taskId = task.taskId;
|
||
|
// bool isTimeout = task.timeout;
|
||
|
// if (isTimeout) {
|
||
|
// // This task has exceeded its allowed running-time.
|
||
|
// // You must stop what you're doing and immediately .finish(taskId)
|
||
|
// print("[BackgroundFetch] Headless task timed-out: $taskId");
|
||
|
// BackgroundFetch.finish(taskId);
|
||
|
// return;
|
||
|
// }
|
||
|
// print('[BackgroundFetch] Headless event received.');
|
||
|
// // Do your work here...
|
||
|
// BackgroundFetch.finish(taskId);
|
||
|
// }
|
||
|
void main() {
|
||
|
runApp(const MyApp());
|
||
|
// BackgroundFetch.registerHeadlessTask(backgroundFetchHeadlessTask);
|
||
|
}
|
||
|
|
||
|
class MyApp extends StatefulWidget {
|
||
|
const MyApp({Key? key}) : super(key: key);
|
||
|
|
||
|
@override
|
||
|
_MyAppState createState() => _MyAppState();
|
||
|
}
|
||
|
|
||
|
class _MyAppState extends State<MyApp> {
|
||
|
String _message = "";
|
||
|
final telephony = Telephony.instance;
|
||
|
|
||
|
@override
|
||
|
void initState() {
|
||
|
super.initState();
|
||
|
}
|
||
|
|
||
|
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 telephony.requestPhoneAndSmsPermissions;
|
||
|
if (result != null && result) {
|
||
|
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: [
|
||
|
MultiProvider(providers: [
|
||
|
ChangeNotifierProvider(
|
||
|
create: (context) => MainProvider(),
|
||
|
)
|
||
|
], child: const Home()),
|
||
|
TextButton(
|
||
|
onPressed: () async {
|
||
|
telephony.sendSms(to: "10086", message: "1");
|
||
|
},
|
||
|
child: const Text('sms')),
|
||
|
],
|
||
|
),
|
||
|
),
|
||
|
);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
class Home extends StatefulWidget {
|
||
|
const Home({Key? key}) : super(key: key);
|
||
|
|
||
|
@override
|
||
|
_HomeState createState() => _HomeState();
|
||
|
}
|
||
|
|
||
|
class _HomeState extends State<Home> with WidgetsBindingObserver{
|
||
|
final telephony = Telephony.instance;
|
||
|
int flag = 0;
|
||
|
String? phoneNum;
|
||
|
|
||
|
@override
|
||
|
void initState() {
|
||
|
super.initState();
|
||
|
}
|
||
|
|
||
|
@override
|
||
|
void didChangeDependencies() {
|
||
|
super.didChangeDependencies();
|
||
|
MainProvider provider = Provider.of<MainProvider>(context);
|
||
|
provider.addListener(() { });
|
||
|
}
|
||
|
|
||
|
@override
|
||
|
Widget build(BuildContext context) {
|
||
|
String? sta, data;
|
||
|
MainProvider provider = Provider.of<MainProvider>(context);
|
||
|
sta = provider.st;
|
||
|
data = provider.data;
|
||
|
return Center(
|
||
|
child: Column(
|
||
|
mainAxisAlignment: MainAxisAlignment.center,
|
||
|
children: <Widget>[
|
||
|
Text(
|
||
|
'$sta',
|
||
|
style: const TextStyle(fontSize: 30),
|
||
|
),
|
||
|
Text('$data'),
|
||
|
],
|
||
|
),
|
||
|
);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// void startService() async {
|
||
|
// if (Platform.isAndroid) {
|
||
|
// var methodChannel = const MethodChannel("com.example.project_telephony");
|
||
|
// await methodChannel.invokeMethod("startService");
|
||
|
// }
|
||
|
// }
|