gpt4 book ai didi

android - flutter : How we can use inbuilt message app for send message using package?

转载 作者:行者123 更新时间:2023-11-29 00:01:22 25 4
gpt4 key购买 nike

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

最佳答案

实际上,要以编程方式发送 SMS,您需要实现平台 channel 并使用 SMSManager 发送 SMS。

例子:

安卓部分:

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

}

Dart 代码:

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

关于android - flutter : How we can use inbuilt message app for send message using package?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49419843/

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