Flutter获取设备电话信息插件telephony_info_plus的使用
Flutter获取设备电话信息插件telephony_info_plus的使用
Telephony Info Plus
插件是一个用于在Flutter应用中获取详细SIM卡信息的插件,支持Android平台。该插件提供了关于设备上SIM卡的各种详细信息,包括:
providerName
mcc
(移动国家代码)mnc
(移动网络代码)isSimRoaming
isESim
signalType
signalStrength
特性
Android
- 返回所有SIM信息的设备运行Android 6.0 (API级别23) 或更高版本。
signalType
和signalStrength
只有在Android 10及以上版本中可用,由于API限制。- 需要在
AndroidManifest.xml
中添加以下权限:<uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
- 重要:在使用插件之前必须请求
READ_PHONE_STATE
和ACCESS_FINE_LOCATION
权限。例如:import 'package:permission_handler/permission_handler.dart'; Future<void> requestPermissions() async { await [ Permission.phone, Permission.location ].request(); }
iOS
由于iOS限制,此插件不支持iOS 16+,只能在Android设备上工作。有关更多细节,请参阅 Apple 文档。
安装
在 pubspec.yaml
文件中添加以下依赖项:
dependencies:
telephony_info_plus: ^0.0.1
然后运行:
flutter pub get
使用
在Dart代码中导入插件:
import 'package:telephony_info_plus/telephony_info_plus.dart';
获取SIM信息:
List<TelephonyInfo>? getSimInfo() async {
try {
return await _telephonyInfoPlusPlugin.getSimInfos();
} catch (e) {
print(e);
}
}
平台特定注意事项
Android
- API级别:只有在运行Android 6.0或更新版本的设备上才能完全实现功能。
- 多张SIM卡:对于具有多张SIM卡的设备,插件会返回一个SIM信息数组。
限制
signalType
和signalStrength
只有在Android 10及以上版本中才受支持。- 由于iOS限制,此插件不支持iOS 16+,只能在Android设备上工作。
示例代码
以下是一个完整的示例代码,展示了如何在Flutter应用中使用 telephony_info_plus
插件来获取SIM信息。
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:telephony_info_plus/telephony_info_plus.dart';
import 'package:telephony_info_plus/telephony_info_plus_platform_interface.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
[@override](/user/override)
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String _platformVersion = 'Unknown';
final _telephonyInfoPlusPlugin = TelephonyInfoPlus();
[@override](/user/override)
void initState() {
super.initState();
initPlatformState();
}
Future<void> initPlatformState() async {
List<TelephonyInfo>? platformVersion;
try {
platformVersion = await _telephonyInfoPlusPlugin.getSimInfos();
} catch (e) {
print(e.toString());
}
if (!mounted) return;
setState(() {
_platformVersion = platformVersion?.toString() ?? 'Error retrieving SIM info';
});
}
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('插件示例应用'),
),
body: Center(
child: Text('运行于: $_platformVersion\n'),
),
),
);
}
}
更多关于Flutter获取设备电话信息插件telephony_info_plus的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter获取设备电话信息插件telephony_info_plus的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
当然,以下是如何在Flutter应用中使用telephony_info_plus
插件来获取设备电话信息的示例代码。telephony_info_plus
是一个流行的Flutter插件,用于访问设备的电话信息,如IMEI号码、SIM卡序列号等。
首先,你需要在你的pubspec.yaml
文件中添加telephony_info_plus
依赖项:
dependencies:
flutter:
sdk: flutter
telephony_info_plus: ^3.0.0 # 请注意版本号,使用最新版本
然后运行flutter pub get
来安装依赖项。
接下来,你需要在你的Flutter应用中请求必要的权限(如读取电话状态权限),并在代码中使用telephony_info_plus
插件来获取设备电话信息。以下是一个完整的示例:
import 'package:flutter/material.dart';
import 'package:telephony_info_plus/telephony_info_plus.dart';
import 'package:permission_handler/permission_handler.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: DeviceInfoScreen(),
);
}
}
class DeviceInfoScreen extends StatefulWidget {
@override
_DeviceInfoScreenState createState() => _DeviceInfoScreenState();
}
class _DeviceInfoScreenState extends State<DeviceInfoScreen> {
late TelephonyInfo _telephonyInfo;
String? _imei;
String? _imeiSv;
String? _simSerialNumber;
String? _subscriberId;
String? _carrierName;
String? _networkType;
String? _lineNumber;
bool _permissionGranted = false;
@override
void initState() {
super.initState();
_telephonyInfo = TelephonyInfo();
_requestPermissions();
}
Future<void> _requestPermissions() async {
var status = await Permission.phone.status;
if (!status.isGranted) {
var result = await Permission.phone.request();
if (result.isGranted) {
setState(() {
_permissionGranted = true;
});
_getDeviceInfo();
}
} else {
setState(() {
_permissionGranted = true;
});
_getDeviceInfo();
}
}
Future<void> _getDeviceInfo() async {
if (_permissionGranted) {
try {
_imei = await _telephonyInfo.imei;
_imeiSv = await _telephonyInfo.imeiSv;
_simSerialNumber = await _telephonyInfo.simSerialNumber;
_subscriberId = await _telephonyInfo.subscriberId;
_carrierName = await _telephonyInfo.carrierName;
_networkType = await _telephonyInfo.networkType;
_lineNumber = await _telephonyInfo.lineNumber;
} catch (e) {
print("Error fetching device info: $e");
}
setState(() {});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Device Info'),
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: _permissionGranted
? Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('IMEI: $_imei'),
SizedBox(height: 10),
Text('IMEI SV: $_imeiSv'),
SizedBox(height: 10),
Text('SIM Serial Number: $_simSerialNumber'),
SizedBox(height: 10),
Text('Subscriber ID: $_subscriberId'),
SizedBox(height: 10),
Text('Carrier Name: $_carrierName'),
SizedBox(height: 10),
Text('Network Type: $_networkType'),
SizedBox(height: 10),
Text('Line Number: $_lineNumber'),
],
)
: Center(
child: Text(
'Waiting for permissions...',
style: TextStyle(fontSize: 20),
),
),
),
);
}
}
说明:
- 权限请求:使用
permission_handler
插件来请求读取电话状态的权限。 - 插件初始化:初始化
TelephonyInfo
对象。 - 获取设备信息:调用
TelephonyInfo
对象的方法获取IMEI号码、SIM卡序列号等信息。 - UI显示:在UI中显示获取到的设备信息。
请确保在真实设备上测试此代码,因为模拟器可能无法提供所有电话信息。同时,注意处理用户拒绝权限请求的情况,并尊重用户的隐私选择。