簡體   English   中英

獲取用戶SIM卡列表並選中-flutter

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

對於flutter應用,需要用用戶的SIM卡發送短信,為此,我希望能夠在雙卡手機中選擇想要的SIM卡。

我檢查了 sms_plugin包,但用戶無法選擇 SIM 購物車,您必須在代碼中選擇。

您可以在 Android API 和 Flutter 之間創建通信通道以獲取 sim 列表。

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

否則你可以使用這個 Flutter 包https://github.com/flutter-moum/flutter_sim_info

你可以試試https://pub.dev/packages/mobile_number這個包,

添加依賴

dependencies:
  mobile_number: ^1.0.4

代碼片段

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

我會傾向於使用這個庫:

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

獲取設備 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 以發送短信


實際選擇一個 SIM 卡來發送消息(在多 SIM 卡手機中)在 Flutter 方面很棘手,並且還取決於用戶的 SIM 卡設置:

  1. 用戶是否設置了默認 SIM 卡或
  2. 每次發送 SMS 時,設備是否都會詢問使用哪個 SIM 卡

大多數軟件包(例如Telephony )似乎將此委托給設備(即允許設備提示用戶選擇 SIM 或僅使用默認 SIM,如果已設置)

如果您想覆蓋它並明確設置要使用的 SIM 卡,您可能需要查看特定於平台的解決方案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM