功能开关

master
王亚玲 2 years ago
parent 991a01c6f8
commit b78169288f

@ -7,12 +7,14 @@ import 'package:call_log/call_log.dart';
import 'package:flutter/cupertino.dart'; import 'package:flutter/cupertino.dart';
import 'package:flutter_background_service/flutter_background_service.dart'; import 'package:flutter_background_service/flutter_background_service.dart';
import 'package:get/get.dart'; import 'package:get/get.dart';
import 'package:project_telephony/utils/hive_store.dart';
import 'package:provider/provider.dart'; import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart'; import 'package:shared_preferences/shared_preferences.dart';
import 'package:telephony/telephony.dart'; import 'package:telephony/telephony.dart';
import '../../providers/user_provider.dart'; import '../../providers/user_provider.dart';
import '../../utils/user_tool.dart';
@ -24,7 +26,7 @@ Future<void> initializeService() async {
// this will be executed when app is in foreground or background in separated isolate // this will be executed when app is in foreground or background in separated isolate
onStart: onStart, onStart: onStart,
// auto start service // auto start service
autoStart: true, autoStart: false,
isForegroundMode: true, isForegroundMode: true,
), ),
iosConfiguration: IosConfiguration( iosConfiguration: IosConfiguration(
@ -53,19 +55,20 @@ void onStart(ServiceInstance service ) async {
String? phoneNum=""; String? phoneNum="";
int? callRecords=0; int? callRecords=0;
String callState; String callState;
// bool? kg= false; service.on('stopService').listen((event) {
service.stopSelf();
});
final Future<SharedPreferences> _prefs = SharedPreferences.getInstance();
final SharedPreferences prefs = await _prefs;
// bool? kg=prefs.getBool("kg");
// print(kg);
Timer.periodic(const Duration(seconds: 1), (timer) async { Timer.periodic(const Duration(seconds: 1), (timer) async {
final SharedPreferences prefs = await SharedPreferences.getInstance();
CallState state = await Telephony.instance.callState; CallState state = await Telephony.instance.callState;
callState = state.name; callState = state.name;
// bool? kg= prefs.getBool("kg");
// bool? kg=prefs.getBool("kg"); // // bool? kg= prefs.getBool("kg");
print("这是数据${prefs.getBool("kg")}");
// print("这是数据$kg"); // print("这是数据$kg");
// kg= await HiveStore.appBox!.get("kg"); // kg = UserTool.userProvider.kg;
// print("这是数据${prefs.getBool("kg")}");
// print(callState!+" $flag"); // print(callState!+" $flag");
if (callState == "IDLE") { if (callState == "IDLE") {
if (flag != 0 ) { if (flag != 0 ) {
@ -74,16 +77,14 @@ void onStart(ServiceInstance service ) async {
final Iterable<CallLogEntry> entry = await CallLog.query(); final Iterable<CallLogEntry> entry = await CallLog.query();
phoneNum=entry.first.number; phoneNum=entry.first.number;
callRecords = entry.first.duration; callRecords = entry.first.duration;
// DateTime.fromMillisecondsSinceEpoch(entry.first.timestamp!) // DateTime.fromMillisecondsSinceEpoch(entry.first.timestamp!)
// print('DURATION : ${entry.first.duration}');/// // print('DURATION : ${entry.first.duration}');///
if(callRecords!=0){ if(callRecords!=0){
print("接通了"); print("接通了");
// if(kg!){ Telephony.backgroundInstance.sendSms(to: phoneNum!, message: "接通了");
// Telephony.backgroundInstance.sendSms(to: phoneNum!, message: "接通了");
// }
}else{ }else{
print("没接通"); print("没接通");
Telephony.backgroundInstance.sendSms(to: phoneNum!, message: "接通了");
// if(kg!) { // if(kg!) {
// Telephony.backgroundInstance.sendSms(to: phoneNum!, message: "没接通"); // Telephony.backgroundInstance.sendSms(to: phoneNum!, message: "没接通");
// } // }

@ -162,6 +162,7 @@ _getBody() {
onTap: () { onTap: () {
print(title); print(title);
if (title == "接听后") { if (title == "接听后") {
Get.to(() => const ContentConnectPage()); Get.to(() => const ContentConnectPage());
} else { } else {
Get.to(() => const ContentRefusePage()); Get.to(() => const ContentRefusePage());

@ -1,7 +1,10 @@
import 'dart:io';
import 'package:bot_toast/bot_toast.dart'; import 'package:bot_toast/bot_toast.dart';
import 'package:flustars/flustars.dart'; import 'package:flustars/flustars.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
import 'package:flutter_background_service/flutter_background_service.dart';
import 'package:permission_handler/permission_handler.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/login/login_page.dart'; import 'package:project_telephony/ui/login/login_page.dart';
@ -43,17 +46,21 @@ class _UserPageState extends State<UserPage> {
final userProvider = Provider.of<UserProvider>(Get.context!, listen: false); final userProvider = Provider.of<UserProvider>(Get.context!, listen: false);
bool isVip = false; bool isVip = false;
String? endDate; String? endDate;
bool vle = UserTool.userProvider.userInfo.isVip==1; late bool vle;
@override @override
void initState() { void initState() {
super.initState(); super.initState();
if ( UserTool.userProvider.isLogin) { if ( UserTool.userProvider.isLogin) {
if (UserTool.userProvider.userInfo.end<=DateTime.now().millisecondsSinceEpoch ) { if (UserTool.userProvider.userInfo.end<=DateTime.now().millisecondsSinceEpoch ) {
isVip = false; isVip = false;
vle =false;
} else { } else {
vle =true;
isVip = true; isVip = true;
} }
endDate=DateUtil.formatDateMs(UserTool.userProvider.userInfo.end * 1000, format: DateFormats.y_mo_d); endDate=DateUtil.formatDateMs(UserTool.userProvider.userInfo.end * 1000, format: DateFormats.y_mo_d);
}else{
vle = false;
} }
} }
@ -261,6 +268,9 @@ class _UserPageState extends State<UserPage> {
// //
_getSwitch2(){ _getSwitch2(){
// final service=FlutterBackgroundService();
// ServiceInstance service;
return Container( return Container(
padding: EdgeInsets.symmetric(horizontal: 32.w), padding: EdgeInsets.symmetric(horizontal: 32.w),
child: ListTile( child: ListTile(
@ -281,24 +291,46 @@ class _UserPageState extends State<UserPage> {
trailing: Switch( trailing: Switch(
value: vle, value: vle,
onChanged: (value) async { onChanged: (value) async {
final service = FlutterBackgroundService();
var isRunning = await service.isRunning();
final SharedPreferences prefs = await _prefs; final SharedPreferences prefs = await _prefs;
await prefs.setBool("kg", value); // if(!value){
print("这是数据${prefs.getBool("kg")}"); //
// }
// await prefs.setBool("kg", value);
// print("这是数据${prefs.getBool("kg")}");
setState(() { setState(() {
if(!isVip){ if(!isVip){
vle=value; vle=value;
// prefs.setBool("kg", vle);
// initializeService();
if(vle){ if(vle){
print("true"); print("true");
UserTool.userProvider.setKg(vle); prefs.remove("kg");
// prefs.setBool("kg", vle); prefs.setBool("kg", vle);
service.startService();
// WidgetsFlutterBinding.ensureInitialized();
// initializeService();
// UserTool.userProvider.setKg(vle)
// initializeService();
// initializeService();
BotToast.showText(text: " 你开启了该功能"); BotToast.showText(text: " 你开启了该功能");
// initializeService();
// print("这是数据${prefs.getBool("kg")}"); // print("这是数据${prefs.getBool("kg")}");
}else{ }else{
print("false"); print("false");
UserTool.userProvider.setKg(vle); prefs.remove("kg");
// prefs.setBool("kg", vle); prefs.setBool("kg", vle);
service.invoke("stopService");
// exit(0);
// UserTool.userProvider.setKg(vle);
// prefs.setBool("kg", false);
// initializeService();
BotToast.showText(text: "你已经关闭该功能"); BotToast.showText(text: "你已经关闭该功能");
// print("这是数据${prefs.getBool("kg")}");
// print("这是数据${UserTool.userProvider.kg}");
} }
}else{ }else{
BotToast.showText(text: "还没有开通会员,不可使用改功能"); BotToast.showText(text: "还没有开通会员,不可使用改功能");

Loading…
Cancel
Save