修复闪退

master
王亚玲 2 years ago
parent af9fb50bd1
commit e2c46a1452

@ -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);
//// }
//}

@ -128,7 +128,6 @@ class MyApp extends StatefulWidget {
} }
class _MyAppState extends State<MyApp> { class _MyAppState extends State<MyApp> {
String _message = "";
// 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;
@ -139,7 +138,7 @@ class _MyAppState extends State<MyApp> {
void initState() { void initState() {
super.initState(); super.initState();
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();
@ -148,7 +147,7 @@ class _MyAppState extends State<MyApp> {
appId: "wxd930ea5d5a228f5f",universalLink:"https://your.univerallink.com/link/ " appId: "wxd930ea5d5a228f5f",universalLink:"https://your.univerallink.com/link/ "
); );
}); });
final inbox = Telephony.instance.getInboxSms(); // final inbox = Telephony.instance.getInboxSms();
// JPush jPush=JPush(); // JPush jPush=JPush();
// jPush.setup( // jPush.setup(
// appKey: "", // appKey: "",

@ -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<String> recipients) async {
try{
String result =
await sendSMS(message: message, recipients: recipients, sendDirect: true);
print(result);
} on PlatformException catch(e){
print(e.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,
),
);
}
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<CallLogEntry> 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;
}

@ -1,13 +1,12 @@
import 'dart:async'; import 'dart:async';
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:project_telephony/ui/home/content_page.dart'; import 'package:project_telephony/ui/home/content_page.dart';
import 'package:project_telephony/utils/headers.dart'; import 'package:project_telephony/utils/headers.dart';
import 'package:telephony/telephony.dart';
import 'call.dart';
class HomePage extends StatefulWidget { class HomePage extends StatefulWidget {
const HomePage({Key? key}) : super(key: key); const HomePage({Key? key}) : super(key: key);
@ -15,103 +14,14 @@ class HomePage extends StatefulWidget {
@override @override
_HomePageState createState() => _HomePageState(); _HomePageState createState() => _HomePageState();
} }
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());
}
}
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();
// 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<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);
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,
//
// );
}
} else if (callState == "RINGING") {
flag++;
} else if (callState == "OFFHOOK") {
flag++;
}
});
}
class Phone {
static Telephony telephony = Telephony.instance;
}
class _HomePageState extends State<HomePage> { class _HomePageState extends State<HomePage> {
@override @override
void initState() { void initState() {
// TODO: implement initState // TODO: implement initState
super.initState(); super.initState();
Future.delayed(const Duration(milliseconds: 0), () async { initializeService();
//Hive.initFlutter;
await initializeService();
});
} }
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {

@ -15,39 +15,32 @@ class PrivacyRightsPage extends StatefulWidget {
_PrivacyRightsPageState createState() => _PrivacyRightsPageState(); _PrivacyRightsPageState createState() => _PrivacyRightsPageState();
} }
final Telephony telephony = Telephony.instance; // final Telephony telephony = Telephony.instance;
// late final bool permissionsGranted; // late final bool permissionsGranted;
// String body = ""; // String body = "";
// @override // @override
// void initState() async { // void initState() async {
// initPlatformState(); // initPlatformState();
// } // }
class _PrivacyRightsPageState extends State<PrivacyRightsPage> {
// final Permission _permission = ;
// final PermissionHandlerPlatform _permissionHandler =
// PermissionHandlerPlatform.instance;
// PermissionStatus _permissionStatus = Permission.phone.request() as PermissionStatus;
// PermissionStatus _permissionStatus2 = Permission.sms.request() as PermissionStatus;
// Future<void> requestPermission(Permission permission) async {
// final status = await _permissionHandler.requestPermissions([permission]);
// setState(() {
// _permissionStatus = status[permission] ?? PermissionStatus.denied;
// });
// }
bool sms=false; bool sms=false;
bool plone=false; bool plone=false;
@override
class _PrivacyRightsPageState extends State<PrivacyRightsPage> {
@override @override
void initState() { void initState() {
super.initState(); super.initState();
_listenForPermissionStatus(); Future.delayed(const Duration(seconds: 0),() async{
await _listenForPermissionStatus();
});
} }
Future<void> _listenForPermissionStatus() async { Future<void> _listenForPermissionStatus() async {
sms=await Permission.sms.request().isGranted; sms=await Permission.sms.request().isGranted;
plone =await Permission.phone.request().isGranted; plone =await Permission.phone.request().isGranted;
setState(() {});
} }
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Scaffold( return Scaffold(
@ -85,6 +78,15 @@ bool plone=false;
_getRights(){ _getRights(){
return ListTile( return ListTile(
onTap: ()async{ onTap: ()async{
if(!(sms && plone)){
print(sms);
print(plone);
openAppSettings();
}else{
print("123123123123");
}
// await Permission.phone.request(); // await Permission.phone.request();
// await Permission.sms.request(); // await Permission.sms.request();
// Map<Permission, PermissionStatus> statuses = await [ // Map<Permission, PermissionStatus> statuses = await [

Loading…
Cancel
Save