我找到了此代码,该代码允许Flutter自动发送短信。 Flutter send sms with device automatically
import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Build;
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);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (checkSelfPermission(
Manifest.permission.SEND_SMS)
!= PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (shouldShowRequestPermissionRationale(
Manifest.permission.SEND_SMS)) {
// Show an explanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
} else {
// No explanation needed, we can request the permission.
requestPermissions(
new String[]{Manifest.permission.SEND_SMS},
0);
// MY_PERMISSIONS_REQUEST_SEND_SMS is an
// app-defined int constant. The callback method gets the
// result of the request.
}
}
}
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", "");
}
}
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case 0: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! Do the
// contacts-related task you need to do.
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
}
return;
}
// other 'case' lines to check for other
// permissions this app might request.
}
}
}
此代码对我有用。我可以在没有打开本机SMS APP的情况下在Android设备上以短促方式发送SMS:)
但是,我在iOS上搜索了相同的代码,因为Flutter中没有可自动发送SMS的Android软件包和iOS sms软件包。
这是需要在Android和iOS之间架起桥梁的颤动代码。
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');
String num="+33XXXXXXXXX";
Future<Null> sendSms()async {
print("SendSMS");
try {
final String result = await platform.invokeMethod('send',<String,dynamic>{"phone":"$num","msg":"Hello! I'm sent programatically."}); //Replace a 'X' with 10 digit phone number
print(result);
} on PlatformException catch (e) {
print(e.toString());
}
}
Future<Null> sendSms2()async {
print("SendSMS");
try {
final String result = await platform.invokeMethod('send',<String,dynamic>{"phone":"$num","msg":"test baby"}); //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 Column(
children: <Widget>[
new FlatButton(onPressed: () => sendSms(), child: const Text("Send SMS")),
new FlatButton(onPressed: () => sendSms2(), child: const Text("Send SMS")),
],
)
);
}
}
编辑:如果不打开本机SMS,iOS将无法自动发送短信...