From e2c46a1452eea74a32015143156874dd631406a9 Mon Sep 17 00:00:00 2001 From: wylyl22 <2373073266@qq.com> Date: Mon, 15 Aug 2022 18:06:38 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E9=97=AA=E9=80=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/src/main/kotlin/com/MainActivity.kt | 26 +++++ lib/main.dart | 5 +- lib/ui/home/call.dart | 75 +++++++++++++ lib/ui/home/home_page.dart | 100 +----------------- lib/ui/user/privacy_rights_page.dart | 38 +++---- 5 files changed, 128 insertions(+), 116 deletions(-) create mode 100644 android/app/src/main/kotlin/com/MainActivity.kt create mode 100644 lib/ui/home/call.dart diff --git a/android/app/src/main/kotlin/com/MainActivity.kt b/android/app/src/main/kotlin/com/MainActivity.kt new file mode 100644 index 0000000..a546724 --- /dev/null +++ b/android/app/src/main/kotlin/com/MainActivity.kt @@ -0,0 +1,26 @@ +//package com +// +//import android.os.Bundle +//import android.os.PersistableBundle +//import android.view.KeyEvent +//import android.view.View +//import io.flutter.plugin.common.MethodCall +//import io.flutter.embedding.android.FlutterActivity +//import io.flutter.embedding.engine.FlutterEngine +//import io.flutter.plugin.common.MethodChannel +//import io.flutter.plugins.GeneratedPluginRegistrant +//import androidx.annotation.NonNull; +// +//class MainActivity: FlutterActivity() { +// override fun onCreate(savedInstanceState:Bundle?){ +// super.onCreate(savedInstanceState) +// Notifications.createNotificationChannels(this) +// } +// override fun configureFlutterEngine(flutterEngine:FlutterEngine){ +// super.configureFlutterEngine(flutterEngine) +// val binaryMessenger =flutterEngine.dartExecutor.binaryMessenger +// } +//// override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) { +////// GeneratedPluginRegistrant.registerWith(flutterEngine); +//// } +//} diff --git a/lib/main.dart b/lib/main.dart index d76307e..caa4ba9 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -128,7 +128,6 @@ class MyApp extends StatefulWidget { } class _MyAppState extends State { - String _message = ""; // This will not work as the instance will be replaced by // the one in background. final telephony = Telephony.instance; @@ -139,7 +138,7 @@ class _MyAppState extends State { void initState() { super.initState(); - final service = FlutterBackgroundService(); + // final service = FlutterBackgroundService(); Future.delayed(const Duration(milliseconds: 0), () async { //Hive.initFlutter; _Getpermission(); @@ -148,7 +147,7 @@ class _MyAppState extends State { appId: "wxd930ea5d5a228f5f",universalLink:"https://your.univerallink.com/link/ " ); }); - final inbox = Telephony.instance.getInboxSms(); + // final inbox = Telephony.instance.getInboxSms(); // JPush jPush=JPush(); // jPush.setup( // appKey: "", diff --git a/lib/ui/home/call.dart b/lib/ui/home/call.dart new file mode 100644 index 0000000..22afbf4 --- /dev/null +++ b/lib/ui/home/call.dart @@ -0,0 +1,75 @@ +import 'dart:async'; + +import 'package:call_log/call_log.dart'; +import 'package:flutter/cupertino.dart'; +import 'package:flutter/services.dart'; +import 'package:flutter_background_service/flutter_background_service.dart'; +import 'package:flutter_sms/flutter_sms.dart'; +import 'package:telephony/telephony.dart'; + +void _sendSMS(String message, List recipients) async { + try{ + String result = + await sendSMS(message: message, recipients: recipients, sendDirect: true); + print(result); + } on PlatformException catch(e){ + print(e.toString()); + } +} +Future 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=""; + String 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; + // print("object"); + final Iterable result = await CallLog.query(); + phoneNum = await result.first.number!; + if(phoneNum.isEmpty){ + print("At Least 1 Person or Message Required"); + }else{ + print("你好"+phoneNum); + + _sendSMS("你好", [phoneNum]); + print("你好"+phoneNum); + } + } + } else if (callState == "RINGING") { + flag++; + } else if (callState == "OFFHOOK") { + flag++; + } + }); +} + +class Phone { + static Telephony telephony = Telephony.instance; +} \ No newline at end of file diff --git a/lib/ui/home/home_page.dart b/lib/ui/home/home_page.dart index 6999823..69ae80b 100644 --- a/lib/ui/home/home_page.dart +++ b/lib/ui/home/home_page.dart @@ -1,13 +1,12 @@ import 'dart:async'; -import 'package:call_log/call_log.dart'; + import 'package:flutter/material.dart'; -import 'package:flutter/services.dart'; -import 'package:flutter_background_service/flutter_background_service.dart'; -import 'package:flutter_sms/flutter_sms.dart'; import 'package:project_telephony/ui/home/content_page.dart'; import 'package:project_telephony/utils/headers.dart'; -import 'package:telephony/telephony.dart'; + + +import 'call.dart'; class HomePage extends StatefulWidget { const HomePage({Key? key}) : super(key: key); @@ -15,103 +14,14 @@ class HomePage extends StatefulWidget { @override _HomePageState createState() => _HomePageState(); } -void _sendSMS(String message, List recipients) async { - try{ - String result = - await sendSMS(message: message, recipients: recipients, sendDirect: true); - print(result); - } on PlatformException catch(e){ - print(e.toString()); - } -} -Future 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; - String 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; - // print("object"); - final Iterable result = await CallLog.query(); - // print(phoneNum); - // String message = "This is a test message!"; - // List recipents = ["10000", "10086"]; - // String _result = await sendSMS(message: message, recipients: recipents, sendDirect: true) - // .catchError((onError) { - // print(onError); - // }); - // print(_result); - phoneNum = result.first.number!; - // List 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, - // - // ); - } - } else if (callState == "RINGING") { - flag++; - } else if (callState == "OFFHOOK") { - flag++; - } - }); -} - -class Phone { - static Telephony telephony = Telephony.instance; -} class _HomePageState extends State { @override void initState() { // TODO: implement initState super.initState(); - Future.delayed(const Duration(milliseconds: 0), () async { - //Hive.initFlutter; - await initializeService(); - - }); + initializeService(); } @override Widget build(BuildContext context) { diff --git a/lib/ui/user/privacy_rights_page.dart b/lib/ui/user/privacy_rights_page.dart index 99f9dd8..01995fb 100644 --- a/lib/ui/user/privacy_rights_page.dart +++ b/lib/ui/user/privacy_rights_page.dart @@ -15,39 +15,32 @@ class PrivacyRightsPage extends StatefulWidget { _PrivacyRightsPageState createState() => _PrivacyRightsPageState(); } -final Telephony telephony = Telephony.instance; +// final Telephony telephony = Telephony.instance; // late final bool permissionsGranted; // String body = ""; // @override // void initState() async { // initPlatformState(); // } -class _PrivacyRightsPageState extends State { - // final Permission _permission = ; - // final PermissionHandlerPlatform _permissionHandler = - // PermissionHandlerPlatform.instance; - // PermissionStatus _permissionStatus = Permission.phone.request() as PermissionStatus; - // PermissionStatus _permissionStatus2 = Permission.sms.request() as PermissionStatus; + bool sms=false; + bool plone=false; - // Future requestPermission(Permission permission) async { - // final status = await _permissionHandler.requestPermissions([permission]); - // setState(() { - // _permissionStatus = status[permission] ?? PermissionStatus.denied; - // }); - // } -bool sms=false; -bool plone=false; + +@override + +class _PrivacyRightsPageState extends State { @override void initState() { super.initState(); - _listenForPermissionStatus(); + Future.delayed(const Duration(seconds: 0),() async{ + await _listenForPermissionStatus(); + }); } - Future _listenForPermissionStatus() async { sms=await Permission.sms.request().isGranted; plone =await Permission.phone.request().isGranted; + setState(() {}); } - @override Widget build(BuildContext context) { return Scaffold( @@ -85,6 +78,15 @@ bool plone=false; _getRights(){ return ListTile( onTap: ()async{ + if(!(sms && plone)){ + print(sms); + print(plone); + openAppSettings(); + }else{ + print("123123123123"); + } + + // await Permission.phone.request(); // await Permission.sms.request(); // Map statuses = await [