获取用户SIM卡的列表并选择它-Flutter

发布于 2025-02-06 19:53:49 字数 231 浏览 2 评论 0 原文

对于flutter应用程序,有必要使用用户的SIM卡发送SMS,为此,我希望可以在Dual-Sim手机中选择所需的SIM卡。

我检查< sms_plugin 包装,但是用户无法选择您必须在代码中选择SIM卡。

For the flutter application, it is necessary to send an SMS with the user's SIM card, and for this purpose, I want it to be possible to select the desired SIM card in dual-SIM phones.

i check sms_plugin package but user can't select SIM cart you must select in code .

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

耶耶耶 2025-02-13 19:53:50

https://pub.dev/packages/mobile_number

您可以尝试

dependencies:
  mobile_number: ^1.0.4

代码snippe 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()
            ],
          ),
        ),
      ),
    );
  }
}

you can try https://pub.dev/packages/mobile_number this package,

Add dependency

dependencies:
  mobile_number: ^1.0.4

Code snippet

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()
            ],
          ),
        ),
      ),
    );
  }
}
失与倦" 2025-02-13 19:53:50

我会倾向使用此库:

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();

I would lean this library to use:

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();
盗琴音 2025-02-13 19:53:50

您可以在Android API和Flutter之间创建通信通道以获取SIM卡列表。

例如。

否则您可以使用此Flutter软件包
https://github.com/flutter-moum/flutter-moum/flutter_sim_info

You can create communication channel between Android API and Flutter to get sim list.

Eg.
https://github.com/flutter-moum/flutter_sim_info/blob/master/android/src/main/java/flutter/moum/sim_info/MethodHandlerImpl.java

or else you can use this Flutter package
https://github.com/flutter-moum/flutter_sim_info

快乐很简单 2025-02-13 19:53:50

获取设备sim信息


使用 autoussdflutter 软件包,以获取SIM卡详细信息的详细信息,以获取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 SIM的详细信息,以将SIM


转到实际选择一个sim以发送消息(在多SIM电话中)在颤音侧很棘手,也取决于用户的SIM卡设置:

  1. 用户是否设置了默认的SIM卡或
  2. 设备要求使用哪个SIM时间发送了

大多数软件包的时间(例如电话)似乎将其委派给了设备(即允许允许设备带来提示,以便用户选择SIM,或者仅使用默认的SIM,如果设置了),

如果要覆盖此信息并明确设置SIM卡,则可能需要查看特定于平台的解决方案。

Get Device SIM Information


Use the AutoUssdFlutter package to get the details of SIM cards

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}");
  }
}

Set SIM To Send SMS


To actually select a SIM to send the message with (in a multi-SIM phone) is tricky on the Flutter side, and also depends on the user's SIM setting:

  1. Has the user set a default SIM card or
  2. Does the device ask which SIM to use every time an SMS is being sent

Most packages (E.g. Telephony) seem to delegate this to the device (I.e. allow the device to bring a prompt for the user to choose the SIM or just use the default SIM, if set)

If you want to override this and explicitly set the SIM to use, you may need to look into a platform specific solution.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文