dart 获取用户SIM卡列表并选择它- flutter

u0njafvf  于 2023-07-31  发布在  Flutter
关注(0)|答案(5)|浏览(361)

对于Flutter应用程序,有必要发送与用户的SIM卡的短信,并为此目的,我希望它可以选择所需的SIM卡在双SIM卡手机。
我检查sms_plugin包,但用户无法选择SIM卡车,您必须在代码中选择。

5lwkijsr

5lwkijsr1#

你可以试试https://pub.dev/packages/mobile_number这个包

添加依赖

dependencies:
  mobile_number: ^1.0.4

字符串

代码片段t

import 'dart:async';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:mobile_number/mobile_number.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _mobileNumber = '';
  List<SimCard> _simCard = <SimCard>[];

  @override
  void initState() {
    super.initState();
    MobileNumber.listenPhonePermission((isPermissionGranted) {
      if (isPermissionGranted) {
        initMobileNumberState();
      } else {}
    });

    initMobileNumberState();
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initMobileNumberState() async {
    if (!await MobileNumber.hasPhonePermission) {
      await MobileNumber.requestPhonePermission;
      return;
    }
    String mobileNumber = '';
    // Platform messages may fail, so we use a try/catch PlatformException.
    try {
      mobileNumber = (await MobileNumber.mobileNumber)!;
      _simCard = (await MobileNumber.getSimCards)!;
    } on PlatformException catch (e) {
      debugPrint("Failed to get mobile number because of '${e.message}'");
    }

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;

    setState(() {
      _mobileNumber = mobileNumber;
    });
  }

  Widget fillCards() {
    List<Widget> widgets = _simCard
        .map((SimCard sim) => Text(
            'Sim Card Number: (${sim.countryPhonePrefix}) - ${sim.number}\nCarrier Name: ${sim.carrierName}\nCountry Iso: ${sim.countryIso}\nDisplay Name: ${sim.displayName}\nSim Slot Index: ${sim.slotIndex}\n\n'))
        .toList();
    return Column(children: widgets);
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Center(
          child: Column(
            children: <Widget>[
              Text('Running on: $_mobileNumber\n'),
              fillCards()
            ],
          ),
        ),
      ),
    );
  }
}

z6psavjg

z6psavjg3#

我想使用这个库:
https://pub.dev/packages/sim_data

import 'package:sim_data/sim_data.dart';

void printSimCardsData() async {
  try {
    SimData simData = await SimDataPlugin.getSimData();
    for (var s in simData.cards) {
      print('Serial number: ${s.serialNumber}');
    }
  } on PlatformException catch (e) {
    debugPrint("error! code: ${e.code} - message: ${e.message}");
  }
}

void main() => printSimCardsData();

字符串

vqlkdk9b

vqlkdk9b4#

获取设备SIM卡信息

使用AutoUssdFlutter包获取SIM卡的详细信息

import 'package:autoussdflutter/autoussdflutter.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();

  final List<Network> networks = await AutoUssdFlutter
    .getInstance()
    .getDeviceSimNetworks();

  for (final network in networks) {
    debugPrint("Name: ${network.name}");
    debugPrint("MCC: ${network.mcc}");
    debugPrint("MNC: ${network.mnc}");
    debugPrint("Country Name: ${network.countryName}");
    debugPrint("Country ISO Code: ${network.countryIsoCode}");
    debugPrint("Country Dial Code: ${network.countryDialCode}");
  }
}

字符串

设置SIM卡发送短信

在Flutter方面,实际选择一个SIM来发送消息(在多SIM手机中)是很棘手的,并且还取决于用户的SIM设置:
1.用户是否设置了默认SIM卡或
1.每次发送短信时,设备是否会询问使用哪个SIM卡
大多数 Package (例如Telephony)似乎将此委派给设备(即允许设备提示用户选择SIM或仅使用默认SIM(如果已设置)
如果您想覆盖此设置并显式设置要使用的SIM卡,则可能需要查看特定于平台的解决方案。

rkkpypqq

rkkpypqq5#

@Sadhik!这使我们能够检测到用户手机中存在的物理西姆斯。如何检测和显示e-sim卡?

相关问题