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

标签 android flutter dart sms dual-sim

对于 flutter 应用,需要用用户的 SIM 卡发送短信,为此,我希望它可以在双 SIM 手机中选择所需的 SIM 卡。

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

最佳答案

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

添加依赖

dependencies:
  mobile_number: ^1.0.4

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

关于android - 获取用户SIM卡列表并选中-flutter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72592081/

相关文章:

android - NameNotFoundException WebView

android - 来自标签的数据未正确传递到新 Activity

charts - 如何在 Flutter 的圆环饼图的孔内添加文本

json - 在Flutter中显示来自API Json响应的结果时出现错误

dart - 事件队列和微任务队列

android - 如何以编程方式转储屏幕?

javascript - 我如何在应用程序内使用 phonegap 重定向到外部站点

http - 在 Flutter 中使用 http.post 和注册表上传图像?

android - 发布apk不会运行

flutter - 如何获得有关应用程序完全关闭的通知