【问题标题】:Flutter : How we can use inbuilt message app for send message using package?Flutter:我们如何使用内置消息应用程序使用包发送消息?
【发布时间】:2018-03-22 03:01:44
【问题描述】:

我想从我的内置默认消息应用程序发送消息,但我不知道如何使用 dart 代码 [flutter]

【问题讨论】:

    标签: android ios dart flutter


    【解决方案1】:

    实际上要以编程方式发送短信,您需要实现一个平台通道并使用 SMSManager 发送短信。

    例子:

    Android 部分:

    首先给AndroidManifest.xml添加适当的权限。

    <uses-permission android:name="android.permission.SEND_SMS" />
    

    然后在你的MainActivity.java:

      public class MainActivity extends FlutterActivity {
      private static final String CHANNEL = "sendSms";
    
      private MethodChannel.Result callResult;
    
      @Override
      protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        GeneratedPluginRegistrant.registerWith(this);
        new MethodChannel(getFlutterView(), CHANNEL).setMethodCallHandler(
                new MethodChannel.MethodCallHandler() {
                  @Override
                  public void onMethodCall(MethodCall call, MethodChannel.Result result) {
                    if(call.method.equals("send")){
                       String num = call.argument("phone");
                       String msg = call.argument("msg");
                       sendSMS(num,msg,result);
                    }else{
                      result.notImplemented();
                    }
                  }
                });
      }
    
      private void sendSMS(String phoneNo, String msg,MethodChannel.Result result) {
          try {
              SmsManager smsManager = SmsManager.getDefault();
              smsManager.sendTextMessage(phoneNo, null, msg, null, null);
              result.success("SMS Sent");
          } catch (Exception ex) {
              ex.printStackTrace();
              result.error("Err","Sms Not Sent","");
          }
      }
    
    }
    

    飞镖代码:

    import 'dart:async';
    import 'package:flutter/material.dart';
    import 'package:flutter/widgets.dart';
    import 'package:flutter/services.dart';
    
    void main() {
      runApp(new MaterialApp(
        title: "Rotation Demo",
        home: new SendSms(),
      ));
    }
    
    
    class SendSms extends StatefulWidget {
      @override
      _SendSmsState createState() => new _SendSmsState();
    }
    
    class _SendSmsState extends State<SendSms> {
      static const platform = const MethodChannel('sendSms');
    
      Future<Null> sendSms()async {
        print("SendSMS");
        try {
          final String result = await platform.invokeMethod('send',<String,dynamic>{"phone":"+91XXXXXXXXXX","msg":"Hello! I'm sent programatically."}); //Replace a 'X' with 10 digit phone number
          print(result);
        } on PlatformException catch (e) {
          print(e.toString());
        }
      }
    
      @override
      Widget build(BuildContext context) {
        return new Material(
          child: new Container(
            alignment: Alignment.center,
            child: new FlatButton(onPressed: () => sendSms(), child: const Text("Send SMS")),
          ),
        );
      }
    }
    

    refrence link for more detail

    【讨论】:

    • 嗨@Prashant Jajal,这对我不起作用收到此错误“MissingPluginException(未找到方法在通道 sendSms 上发送的实现)”请帮助我谢谢。
    猜你喜欢
    • 1970-01-01
    • 2022-10-20
    • 2019-09-17
    • 2018-11-23
    • 2021-12-05
    • 2017-03-01
    • 2020-10-25
    • 1970-01-01
    • 2019-08-03
    相关资源
    最近更新 更多