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

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

问题描述:

我实际上是在尝试通过Flutter应用发送短信,而无需用户进行交互。
我知道我可以使用url_launcher启动SMS应用程序,但是我实际上是想发送没有用户交互的SMS或从我的flutter应用程序启动SMS。

I am actually trying to send SMS from my flutter app without user's interaction. I know I can launch the SMS app using url_launcher but I actually want to send an SMS without user's interaction or launching SMS from my flutter app.

请可以有人告诉我这是否可能。

Please can someone tell me if this is possible.

非常感谢,
Mahi

Many Thanks, Mahi

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

Actually to send an SMS programatically, you'll need to implement a platform channel and use SMSManager to send SMS.

示例:

Android部分:

Android Part:

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

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

然后在 MainActivity.java 中:

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

}

Dart Code :

Dart Code:

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。示例代码未显示如何处理版本低于code的android设备上的权限> 6.0 及更高版本。如果与 6.0 一起使用,请执行权限调用代码。

1.The example code dosen't show how to handle permission on android devices with version 6.0 and above. If using with 6.0 implement the right permission invoking code.

2。该示例也没有实现选择sim如果是双SIM卡手机。如果在双SIM卡手机上未为短信设置默认SIM卡,则可能不会发送短信。

2.The example also dosen't implement choosing sim incase of dual sim handsets. If no default sim is set for sms on dual sim handsets, sms might not be sent.