简体   繁体   English

获取用户SIM卡列表并选中-flutter

[英]Get the list of user SIM cards and select it - flutter

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.对于flutter应用,需要用用户的SIM卡发送短信,为此,我希望能够在双卡手机中选择想要的SIM卡。

i check sms_plugin package but user can't select SIM cart you must select in code .我检查了 sms_plugin包,但用户无法选择 SIM 购物车,您必须在代码中选择。

You can create communication channel between Android API and Flutter to get sim list.您可以在 Android API 和 Flutter 之间创建通信通道以获取 sim 列表。

Eg.例如。 https://github.com/flutter-moum/flutter_sim_info/blob/master/android/src/main/java/flutter/moum/sim_info/MethodHandlerImpl.java 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否则你可以使用这个 Flutter 包https://github.com/flutter-moum/flutter_sim_info

you can try https://pub.dev/packages/mobile_number this package,你可以试试https://pub.dev/packages/mobile_number这个包,

Add dependency添加依赖

dependencies:
  mobile_number: ^1.0.4

Code 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()
            ],
          ),
        ),
      ),
    );
  }
}

I would lean this library to use:我会倾向于使用这个库:

https://pub.dev/packages/sim_data 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();

Get Device SIM Information获取设备 SIM 信息


Use the AutoUssdFlutter package to get the details of SIM cards使用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}");
  }
}

Set SIM To Send SMS设置 SIM 以发送短信


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:实际选择一个 SIM 卡来发送消息(在多 SIM 卡手机中)在 Flutter 方面很棘手,并且还取决于用户的 SIM 卡设置:

  1. Has the user set a default SIM card or用户是否设置了默认 SIM 卡或
  2. Does the device ask which SIM to use every time an SMS is being sent每次发送 SMS 时,设备是否都会询问使用哪个 SIM 卡

Most packages (Eg Telephony ) seem to delegate this to the device (Ie allow the device to bring a prompt for the user to choose the SIM or just use the default SIM, if set)大多数软件包(例如Telephony )似乎将此委托给设备(即允许设备提示用户选择 SIM 或仅使用默认 SIM,如果已设置)

If you want to override this and explicitly set the SIM to use, you may need to look into a platform specific solution.如果您想覆盖它并明确设置要使用的 SIM 卡,您可能需要查看特定于平台的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM