发送短信

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

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

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

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

@ -1,56 +1,72 @@
import 'dart:async';
import 'dart:io';
import 'dart:ui';
import 'package:call_log/call_log.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_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 {
// 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());
// }
await sendSMS(message: message, recipients: recipients, sendDirect: true);
try{
String result =
await sendSMS(message: message, recipients: recipients, sendDirect: true);
print(result);
} catch (error){
print(error.toString());
}
}
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,
),
);
}
SharedPreferences preferences = await SharedPreferences.getInstance();
final service = FlutterBackgroundService();
void onIosBackground() {
await service.configure(
androidConfiguration: AndroidConfiguration(
// this will be executed when app is in foreground or background in separated isolate
onStart: onStart,
// auto start service
autoStart: false,
isForegroundMode: true,
),
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();
}
// }
bool onIosBackground(ServiceInstance service) {
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;
String phoneNum="";
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.name;
// print(callState!+" $flag");
@ -63,11 +79,9 @@ void onStart() async{
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);
print("你好$phoneNum");
// print("你好123123$phoneNum");
}
}
} else if (callState == "RINGING") {
@ -75,5 +89,5 @@ void onStart() async{
} else if (callState == "OFFHOOK") {
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: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: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/ui/home/content_details_page.dart';
import 'package:project_telephony/ui/widget/centertipsalterwidget.dart';
import 'package:project_telephony/ui/widget/plone_back_button.dart';
import 'package:project_telephony/utils/headers.dart';
import 'package:telephony/telephony.dart';
import '../user/privacy_rights_page.dart';
class ContentPage extends StatefulWidget {
final bool? isAnswer; //truefalse
@ -77,6 +71,9 @@ class _ContentPageState extends State<ContentPage> {
},
));
} 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;
// print(permissionsGranted);
// if(permissionsGranted!){

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

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

@ -1,10 +1,10 @@
import 'package:flutter/material.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/ui/widget/plone_back_button.dart';
import 'package:project_telephony/utils/headers.dart';
import 'package:telephony/telephony.dart';
class PrivacyRightsPage extends StatefulWidget {
final String name;

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

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

@ -41,7 +41,8 @@ dependencies:
call_log: ^4.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
# 屏幕适配
flutter_screenutil: ^5.5.3+2

Loading…
Cancel
Save