app-starlock/lib/tools/NativeInteractionTool.dart

68 lines
2.2 KiB
Dart
Raw Permalink Normal View History

import 'package:flutter/services.dart';
2024-06-04 15:48:39 +08:00
import 'package:star_lock/flavors.dart';
2025-02-13 10:56:24 +08:00
import 'package:star_lock/tools/push/xs_jPhush.dart';
2024-04-26 15:38:59 +08:00
import '../app_settings/app_settings.dart';
///原生交互配置
class NativeInteractionConfig {
static String methodSendChannel = 'starLockFlutterSend';
static String receiveEventChannel = 'starLockFlutterReceive';
}
///原生交互flutter向原生发送消息
typedef BlockBlueStatus = void Function(String status);
class NativeInteractionTool {
MethodChannel sendChannel =
MethodChannel(NativeInteractionConfig.methodSendChannel);
MethodChannel receiveChannel =
MethodChannel(NativeInteractionConfig.receiveEventChannel);
///加载原生分享
void loadNativeShare({required String shareText}) {
2024-06-04 15:48:39 +08:00
final String urlToShare = '${F.apiPrefix}/apps';
2024-06-21 09:28:25 +08:00
sendChannel.invokeMethod('loadNativeShare',
<String, String>{'shareText': shareText, 'urlToShare': urlToShare});
}
///加载原生文件分享
void loadNativeFileShare({required String shareText}) {
sendChannel.invokeMethod('loadNativeShare',
<String, String>{'shareText': shareText, 'urlToShare': 'fileShare'});
}
///获取设备蓝牙状态
void sendGetBlueStatus() {
sendChannel.invokeMethod('sendGetBlueStatus');
}
///获取设备蓝牙是否打开
void receiveChannelBlueIsOnEvent(BlockBlueStatus blockBlueStatus) {
receiveChannel.setMethodCallHandler((MethodCall call) async {
2024-08-19 15:24:14 +08:00
// AppLog.log('收到原生发送的信息call.method: ${call.method} call.arguments:${call.arguments}');
switch (call.method) {
case 'getBlueStatus':
// 获取设备蓝牙开启/关闭状态
final String message = call.arguments;
blockBlueStatus(message);
2024-08-19 15:24:14 +08:00
// AppLog.log('收到原生发送的信息getBlueStatus: $message');
break;
default:
throw MissingPluginException();
}
});
}
2025-02-13 10:56:24 +08:00
Future<String?> getBundleIdentifier() async {
try {
final String? bundleIdentifier =
await sendChannel.invokeMethod('getBundleIdentifier');
return bundleIdentifier;
} on PlatformException catch (e) {
print("Failed to get bundle identifier: '${e.message}'.");
return null;
}
}
}