功能开关

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_background_service/flutter_background_service.dart';
import 'package:get/get.dart';
import 'package:project_telephony/utils/hive_store.dart';
import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:telephony/telephony.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
onStart: onStart,
// auto start service
autoStart: true,
autoStart: false,
isForegroundMode: true,
),
iosConfiguration: IosConfiguration(
@ -53,19 +55,20 @@ void onStart(ServiceInstance service ) async {
String? phoneNum="";
int? callRecords=0;
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 {
final SharedPreferences prefs = await SharedPreferences.getInstance();
CallState state = await Telephony.instance.callState;
callState = state.name;
// bool? kg= prefs.getBool("kg");
// // bool? kg= prefs.getBool("kg");
print("这是数据${prefs.getBool("kg")}");
// print("这是数据$kg");
// kg= await HiveStore.appBox!.get("kg");
// kg = UserTool.userProvider.kg;
// print("这是数据${prefs.getBool("kg")}");
// print(callState!+" $flag");
if (callState == "IDLE") {
if (flag != 0 ) {
@ -74,16 +77,14 @@ void onStart(ServiceInstance service ) async {
final Iterable<CallLogEntry> entry = await CallLog.query();
phoneNum=entry.first.number;
callRecords = entry.first.duration;
// DateTime.fromMillisecondsSinceEpoch(entry.first.timestamp!)
// print('DURATION : ${entry.first.duration}');///
if(callRecords!=0){
print("接通了");
// if(kg!){
// Telephony.backgroundInstance.sendSms(to: phoneNum!, message: "接通了");
// }
Telephony.backgroundInstance.sendSms(to: phoneNum!, message: "接通了");
}else{
print("没接通");
Telephony.backgroundInstance.sendSms(to: phoneNum!, message: "接通了");
// if(kg!) {
// Telephony.backgroundInstance.sendSms(to: phoneNum!, message: "没接通");
// }

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

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

Loading…
Cancel
Save