gpt4 book ai didi

android - 在没有用户交互的情况下从 Flutter 应用程序发送短信?

转载 作者:行者123 更新时间:2023-12-03 04:14:56 25 4
gpt4 key购买 nike

我有一个固定电话号码,我想通过我的 Flutter 应用在​​该号码上发送短信。我怎样才能做到这一点?

最佳答案

您可以在下面复制粘贴运行完整代码
您可以使用包 https://pub.dev/packages/sms_maintained

代码 fragment

    SmsSender sender = SmsSender();
String address = "1234567";

SmsMessage message = SmsMessage(address, 'Hello flutter!');
message.onStateChanged.listen((state) {
if (state == SmsMessageState.Sent) {
print("SMS is sent!");
setState(() {
_message = "SMS is sent";
});
} else if (state == SmsMessageState.Delivered) {
print("SMS is delivered!");
setState(() {
_message = "SMS is delivered!";
});
}
});
sender.sendSms(message);

工作演示

enter image description here

输出

D/flutter_sms(31553): Sent result: Activity.RESULT_OK
I/flutter (31553): SMS is sent!

完整代码

import 'package:flutter/material.dart';
import 'package:sms/sms.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}

class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);

final String title;

@override
_MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;

void _incrementCounter() {
SmsSender sender = SmsSender();
String address = "1234567";

SmsMessage message = SmsMessage(address, 'Hello flutter!');
message.onStateChanged.listen((state) {
if (state == SmsMessageState.Sent) {
print("SMS is sent!");
} else if (state == SmsMessageState.Delivered) {
print("SMS is delivered!");
}
});
sender.sendSms(message);

setState(() {
_counter++;
});
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}

关于android - 在没有用户交互的情况下从 Flutter 应用程序发送短信?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60356974/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com