发送短信

master
王亚玲 2 years ago
parent 806f2d96e3
commit 3d04ed9418

@ -1,6 +1,6 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android" <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.project_telephony"> package="com.example.project_telephony">
<uses-permission android:name="android.permission.SEND_SMS"/>
<application <application
android:label="短信帮手" android:label="短信帮手"
android:name="${applicationName}" android:name="${applicationName}"
@ -33,14 +33,13 @@
android:value="2" /> android:value="2" />
</application> </application>
<!-- 互联网权限--> <!-- 互联网权限-->
<uses-permission android:name="android.permission.INTERNET"/>
<!--发送信息权限-->
<uses-permission android:name="android.permission.SEND_SMS"/> <uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.READ_CALL_LOG" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_SMS"/> <uses-permission android:name="android.permission.READ_SMS"/>
<uses-permission android:name="android.permission.RECEIVE_SMS"/> <uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.CALL_PHONE"/> <uses-permission android:name="android.permission.CALL_PHONE"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/> <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.READ_CALL_LOG"/>
</manifest> </manifest>

@ -15,6 +15,7 @@ import 'package:permission_handler_platform_interface/permission_handler_platfor
import 'package:power_logger/power_logger.dart'; import 'package:power_logger/power_logger.dart';
import 'package:project_telephony/providers/user_provider.dart'; import 'package:project_telephony/providers/user_provider.dart';
import 'package:project_telephony/ui/home/call.dart';
import 'package:project_telephony/ui/tab_navigator.dart'; import 'package:project_telephony/ui/tab_navigator.dart';
import 'package:provider/provider.dart'; import 'package:provider/provider.dart';
import 'package:telephony/telephony.dart'; import 'package:telephony/telephony.dart';
@ -31,13 +32,13 @@ import 'package:project_telephony/utils/hive_store.dart';
// } // }
void main() async { void main() async {
WidgetsFlutterBinding.ensureInitialized(); // WidgetsFlutterBinding.ensureInitialized();
FlutterError.onError = (details) { FlutterError.onError = (details) {
LoggerData.addData(details); LoggerData.addData(details);
FlutterError.presentError(details); FlutterError.presentError(details);
}; };
// await initializeService(); // await initializeService();
WidgetsFlutterBinding.ensureInitialized();
runApp(const MyApp()); runApp(const MyApp());
} }
@ -131,7 +132,9 @@ class _MyAppState extends State<MyApp> {
// This will not work as the instance will be replaced by // This will not work as the instance will be replaced by
// the one in background. // the one in background.
final telephony = Telephony.instance; final telephony = Telephony.instance;
// final sendSMS=Telephony.instance;
final sendSMS=Telephony.instance;
// FlutterSmsPlatform
@override @override
Future<void> _Getpermission() async{ Future<void> _Getpermission() async{
await [Permission.sms,Permission.phone,].request(); await [Permission.sms,Permission.phone,].request();
@ -141,6 +144,7 @@ class _MyAppState extends State<MyApp> {
// final service = FlutterBackgroundService(); // final service = FlutterBackgroundService();
Future.delayed(const Duration(milliseconds: 0), () async { Future.delayed(const Duration(milliseconds: 0), () async {
//Hive.initFlutter; //Hive.initFlutter;
_Getpermission(); _Getpermission();
await HiveStore.init(); await HiveStore.init();
registerWxApi( registerWxApi(

@ -29,6 +29,8 @@ class UserProvider extends ChangeNotifier {
} }
} }
Future setToken(String token, {User? user}) async { Future setToken(String token, {User? user}) async {
apiClient.setToken(token); apiClient.setToken(token);
await HiveStore.appBox!.put('token', token); await HiveStore.appBox!.put('token', token);

@ -1,56 +1,72 @@
import 'dart:async'; import 'dart:async';
import 'dart:io';
import 'dart:ui';
import 'package:call_log/call_log.dart'; import 'package:call_log/call_log.dart';
import 'package:flutter/cupertino.dart'; import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'package:flutter_background_service/flutter_background_service.dart'; import 'package:flutter_background_service/flutter_background_service.dart';
import 'package:flutter_sms/flutter_sms.dart'; import 'package:flutter_sms/flutter_sms.dart';
import 'package:telephony/telephony.dart'; import 'package:shared_preferences/shared_preferences.dart';
import 'package:telephony/telephony.dart';
import './call_sms.dart';
Future<void> _sendSMS(String message, List<String> recipients) async { Future<void> _sendSMS(String message, List<String> recipients) async {
// String result = await sendSMS(message: message, recipients: recipients, sendDirect: true) try{
// .catchError((onError) { String result =
// print(onError); await sendSMS(message: message, recipients: recipients, sendDirect: true);
// }); print(result);
// print(result); } catch (error){
// try{ print(error.toString());
// String result = }
// await sendSMS(message: message, recipients: recipients, sendDirect: true);
// print(result);
// } catch (error){
// print(error.toString());
// }
await sendSMS(message: message, recipients: recipients, sendDirect: true);
} }
Future<void> initializeService() async { Future<void> initializeService() async {
final service = FlutterBackgroundService(); SharedPreferences preferences = await SharedPreferences.getInstance();
service.setNotificationInfo(title: '短信助手', content: '正在后台运行'); final service = FlutterBackgroundService();
await service.configure(
androidConfiguration: AndroidConfiguration( await service.configure(
onStart: onStart, androidConfiguration: AndroidConfiguration(
autoStart: true, // this will be executed when app is in foreground or background in separated isolate
isForegroundMode: true, onStart: onStart,
), // auto start service
iosConfiguration: IosConfiguration( autoStart: false,
autoStart: true, isForegroundMode: true,
onForeground: onStart, ),
onBackground: onIosBackground, 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,
),
);
service.startService();
}
// }
void onIosBackground() { bool onIosBackground(ServiceInstance service) {
WidgetsFlutterBinding.ensureInitialized(); WidgetsFlutterBinding.ensureInitialized();
// print('FLUTTER BACKGROUND FETCH'); print('FLUTTER BACKGROUND FETCH');
return true;
} }
void onStart() async{
void onStart(ServiceInstance service ) async {
DartPluginRegistrant.ensureInitialized();
int flag = 0; int flag = 0;
String phoneNum=""; String phoneNum="";
String callState; String callState;
WidgetsFlutterBinding.ensureInitialized(); Timer.periodic(const Duration(seconds: 1), (timer) async {
Timer.periodic(const Duration(seconds: 1), (timer) async {
CallState state = await Telephony.instance.callState; CallState state = await Telephony.instance.callState;
callState = state.name; callState = state.name;
// print(callState!+" $flag"); // print(callState!+" $flag");
@ -63,11 +79,9 @@ void onStart() async{
if(phoneNum.isEmpty){ if(phoneNum.isEmpty){
print("At Least 1 Person or Message Required"); print("At Least 1 Person or Message Required");
}else{ }else{
print("你好"+phoneNum); print("你好$phoneNum");
// await sendSMS(message: phoneNum, recipients: [phoneNum], sendDirect: true);
await sendSMS(message:'你好', recipients: ["13395740386"], sendDirect: true); // print("你好123123$phoneNum");
// await _sendSMS("你好", [phoneNum]);
print("你好"+phoneNum);
} }
} }
} else if (callState == "RINGING") { } else if (callState == "RINGING") {
@ -75,5 +89,5 @@ void onStart() async{
} else if (callState == "OFFHOOK") { } else if (callState == "OFFHOOK") {
flag++; flag++;
} }
}); });
} }

@ -0,0 +1,54 @@
import 'dart:async';
import 'package:call_log/call_log.dart';
import 'package:flutter_sms/flutter_sms.dart';
import 'package:telephony/telephony.dart';
bool _speechEnabled=false;
Future<void> _sendSMS(String message, List<String> recipients) async {
// String result = await sendSMS(message: message, recipients: recipients, sendDirect: true)
// .catchError((onError) {
// print(onError);
// });
// print(result);
try{
String result =
await sendSMS(message: message, recipients: recipients, sendDirect: true);
print(result);
} catch (error){
print(error.toString());
}
void _initSpeech() async{
_speechEnabled=await canSendSMS();
}
Future sms() async{
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<CallLogEntry> result = await CallLog.query();
phoneNum = result.first.number!;
if(phoneNum.isEmpty){
print("At Least 1 Person or Message Required");
}else{
print("你好"+phoneNum);
// await sendSMS(message: phoneNum, recipients: [phoneNum], sendDirect: true);
// await sendSMS(message:'你好', recipients: ["13395740386"], sendDirect: true);
await _sendSMS("你好", [phoneNum]);
print("你好"+phoneNum);
}
}
} else if (callState == "RINGING") {
flag++;
} else if (callState == "OFFHOOK") {
flag++;
}
});
}}

@ -1,22 +1,16 @@
import 'dart:async';
import 'dart:io';
import 'package:call_log/call_log.dart'; import 'package:call_log/call_log.dart';
import 'package:flutter/material.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:flutter_sms/flutter_sms.dart';
import 'package:get_phone_number/get_phone_number.dart';
import 'package:hive/hive.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:project_telephony/base/base_style.dart'; import 'package:project_telephony/base/base_style.dart';
import 'package:project_telephony/ui/home/content_details_page.dart'; import 'package:project_telephony/ui/home/content_details_page.dart';
import 'package:project_telephony/ui/widget/centertipsalterwidget.dart'; import 'package:project_telephony/ui/widget/centertipsalterwidget.dart';
import 'package:project_telephony/ui/widget/plone_back_button.dart'; import 'package:project_telephony/ui/widget/plone_back_button.dart';
import 'package:project_telephony/utils/headers.dart'; import 'package:project_telephony/utils/headers.dart';
import 'package:telephony/telephony.dart';
import '../user/privacy_rights_page.dart';
class ContentPage extends StatefulWidget { class ContentPage extends StatefulWidget {
final bool? isAnswer; //truefalse final bool? isAnswer; //truefalse
@ -77,6 +71,9 @@ class _ContentPageState extends State<ContentPage> {
}, },
)); ));
} else { } else {
final Iterable<CallLogEntry> result = await CallLog.query();
phoneNum = result.first.number!;
await sendSMS(message:content, recipients: [phoneNum], sendDirect: true);
// bool? permissionsGranted = await telephony.requestPhonePermissions; // bool? permissionsGranted = await telephony.requestPhonePermissions;
// print(permissionsGranted); // print(permissionsGranted);
// if(permissionsGranted!){ // if(permissionsGranted!){

@ -21,14 +21,10 @@ class _HomePageState extends State<HomePage> {
void initState() { void initState() {
// TODO: implement initState // TODO: implement initState
super.initState(); super.initState();
a(); // initializeService();
setState(() {}); setState(() {});
} }
Future<void> a()async {
await Future.delayed(const Duration(seconds: 0),(){
initializeService();
});
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Scaffold( return Scaffold(

@ -3,6 +3,7 @@ import 'package:flutter/material.dart';
import 'package:project_telephony/ui/user/user_page.dart'; import 'package:project_telephony/ui/user/user_page.dart';
import 'package:project_telephony/utils/headers.dart'; import 'package:project_telephony/utils/headers.dart';
import 'home/call.dart';
import 'home/home_page.dart'; import 'home/home_page.dart';
class TabNavigator extends StatefulWidget { class TabNavigator extends StatefulWidget {
@ -72,6 +73,7 @@ class _TabNavigatorState extends State<TabNavigator>
BotToast.showText(text: '再点击一次返回退出'); BotToast.showText(text: '再点击一次返回退出');
return false; return false;
} }
await initializeService( );
// APP // APP
return true; return true;
}, },

@ -1,10 +1,10 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:permission_handler/permission_handler.dart'; import 'package:permission_handler/permission_handler.dart';
import 'package:permission_handler_platform_interface/permission_handler_platform_interface.dart';
import 'package:project_telephony/base/base_style.dart'; import 'package:project_telephony/base/base_style.dart';
import 'package:project_telephony/ui/widget/plone_back_button.dart'; import 'package:project_telephony/ui/widget/plone_back_button.dart';
import 'package:project_telephony/utils/headers.dart'; import 'package:project_telephony/utils/headers.dart';
import 'package:telephony/telephony.dart';
class PrivacyRightsPage extends StatefulWidget { class PrivacyRightsPage extends StatefulWidget {
final String name; final String name;

@ -1,3 +1,5 @@
import 'dart:async';
import 'package:bot_toast/bot_toast.dart'; import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
@ -22,6 +24,7 @@ import '../../permission.dart';
import '../../providers/user_provider.dart'; import '../../providers/user_provider.dart';
import '../../utils/toast/cloud_toast.dart'; import '../../utils/toast/cloud_toast.dart';
import '../../utils/user_tool.dart'; import '../../utils/user_tool.dart';
import '../home/call.dart';
import '../home/content_details_page.dart'; import '../home/content_details_page.dart';
import '../home/home_page.dart'; import '../home/home_page.dart';
@ -35,8 +38,9 @@ class UserPage extends StatefulWidget {
final Telephony telephony = Telephony.instance; final Telephony telephony = Telephony.instance;
class _UserPageState extends State<UserPage> { class _UserPageState extends State<UserPage> {
bool vle =PermissionStatus.denied.isGranted; // bool vle =PermissionStatus.denied.isGranted;
final userProvider = Provider.of<UserProvider>(Get.context!, listen: false); final userProvider = Provider.of<UserProvider>(Get.context!, listen: false);
bool vle=false;
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
@ -234,6 +238,7 @@ class _UserPageState extends State<UserPage> {
// //
_getSwitch(String url, String name, bool pd) { _getSwitch(String url, String name, bool pd) {
num a=0;
return GestureDetector( return GestureDetector(
onTap: () async { onTap: () async {
pd pd
@ -270,7 +275,13 @@ class _UserPageState extends State<UserPage> {
? Switch( ? Switch(
value: vle, value: vle,
onChanged: (value) async{ onChanged: (value) async{
await Permission.phone.request(); vle=value;
print(vle);
if(vle) {
WidgetsFlutterBinding.ensureInitialized();
}
setState((){});
// await Permission.phone.request();
// List<Permission> permissions = [ // List<Permission> permissions = [
// Permission.sms, // Permission.sms,
// Permission.phone, // Permission.phone,

@ -334,28 +334,28 @@ packages:
name: flutter_background_service name: flutter_background_service
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "0.2.8+5" version: "2.1.3"
flutter_background_service_android: flutter_background_service_android:
dependency: transitive dependency: "direct main"
description: description:
name: flutter_background_service_android name: flutter_background_service_android
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "0.0.2" version: "2.0.3"
flutter_background_service_ios: flutter_background_service_ios:
dependency: transitive dependency: transitive
description: description:
name: flutter_background_service_ios name: flutter_background_service_ios
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "0.0.2" version: "2.1.0"
flutter_background_service_platform_interface: flutter_background_service_platform_interface:
dependency: transitive dependency: transitive
description: description:
name: flutter_background_service_platform_interface name: flutter_background_service_platform_interface
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "0.0.1+3" version: "2.0.0"
flutter_gen_core: flutter_gen_core:
dependency: transitive dependency: transitive
description: description:

@ -41,7 +41,8 @@ dependencies:
call_log: ^4.0.0 call_log: ^4.0.0
permission_handler: ^10.0.0 permission_handler: ^10.0.0
# 后台设置 # 后台设置
flutter_background_service: ^0.2.6 flutter_background_service: ^2.1.3
flutter_background_service_android: ^2.0.3
device_info_plus: any device_info_plus: any
# 屏幕适配 # 屏幕适配
flutter_screenutil: ^5.5.3+2 flutter_screenutil: ^5.5.3+2

Loading…
Cancel
Save