首页 文章

如何在没有用户互动的情况下发送短信?

提问于
浏览
6

我实际上是试图从我的扑动应用程序发送短信,而无需用户的互动 . 我知道我可以使用url_launcher启动短信应用程序但我实际上想要在没有用户交互的情况下发送短信或从我的颤动应用程序启动短信 .

有人可以告诉我这是否可行 .

非常感谢,Mahi

1 回答

  • 8

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

    例:

    Android部分:

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

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

    然后在你的_2865846中:

    package com.yourcompany.example;
    
    import android.os.Bundle;
    import android.telephony.SmsManager;
    import android.util.Log;
    import io.flutter.app.FlutterActivity;
    import io.flutter.plugin.common.MethodCall;
    import io.flutter.plugin.common.MethodChannel;
    import io.flutter.plugins.GeneratedPluginRegistrant;
    
    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")),
          ),
        );
      }
    }
    

    希望这有帮助!

    ** 注意:

    1.示例代码不显示如何处理版本为 6.0 及以上版本的Android设备的权限 . 如果使用 6.0 实现正确的权限调用代码 .

    这个例子也没有实现选择双卡手机的SIM卡 . 如果在双卡手机上没有为短信设置默认SIM卡,则可能不会发送短信 .

相关问题