简体   繁体   English

如何通过按钮发送直接短信

[英]How can I send a direct SMS message with a button

My end goal is to create an application that will send a text message to a database of phone numbers that the user inputs. 我的最终目标是创建一个应用程序,该应用程序将向用户输入的电话号码数据库发送文本消息。 Then, by pressing a button they can automatically respond to all people that they are driving, and would like them to text them back another time. 然后,通过按下按钮,他们可以自动响应所驾驶的所有人,并希望他们再次发短信给他们。 The automatic message response is also customizable. 自动消息响应也可以自定义。 I'm using Xamarin for Visual Studios, and am struggling making a text to send. 我正在使用Xamarin for Visual Studios,并且正在努力发送文本。

using Android.App;
using Android.Widget;
using Android.OS;
using System;
using Android.Telephony;

namespace DrivePhone_GeniusHourProject
{
    [Activity(Label = "Drive Time!", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            // Set our view from the "main" layout resource
            SetContentView (Resource.Layout.Main);

            EditText SMS = FindViewById<EditText>(Resource.Id.SMS_Message);
            Button sendSMS = FindViewById<Button>(Resource.Id.Send_SMS);

            string SMS_Text = SMS.ToString();
            var smsTo = NSUrl.FromString();
            SmsMessage();

            smsTo = "815376377";
            sendSMS.Click += (object sender, EventArgs e) =>
            {

            }
        }
    }
}

My problem is that i'll do SmsManager. 我的问题是我要做SmsManager。 and then there isn't an option to send text. 然后就没有发送文本的选项。 But in develoers website there is a public method called sendTextMessage(); 但是在develoers网站上,有一个名为sendTextMessage()的公共方法。 But I can't use it in Visual studio. 但是我不能在Visual Studio中使用它。

In Xamarin, it is Android.Telephony.Gsm.SmsManager.SendTextMessage Method . 在Xamarin中,它是Android.Telephony.Gsm.SmsManager.SendTextMessage Method

For example: 例如:

sendSMS.Click += (object sender, EventArgs e) =>
{
   var content = "**HERE GOES SMS CONTENT**";
   var destinationAdd = "**HERE GOES DESTINATION PHONE NUMBER**";

   Android.Telephony.Gsm.SmsManager sm = SmsManager.Default;
   sm.SendTextMessage(destinationAdd, null, content, null, null);
}

Don't forget to add permission in your manifest: 不要忘记在清单中添加权限:

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

If you want to receive sms, you also need the following permission: 如果要接收短信,还需要以下权限:

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

For a better way to sent message install xam.plugin.Messaging( https://www.nuget.org/packages/Xam.Plugins.Messaging/ ) 为了更好地发送消息,请安装xam.plugin.Messaging( https://www.nuget.org/packages/Xam.Plugins.Messaging/

Then use the below code to send message 然后使用下面的代码发送消息

var smsMessenger = CrossMessaging.Current.SmsMessenger; var smsMessenger = CrossMessaging.Current.SmsMessenger; if (smsMessenger.CanSendSms) smsMessenger.SendSms("+1234567890", "Well hello there from Xam.Messaging.Plugin"); 如果(smsMessenger.CanSendSms)smsMessenger.SendSms(“ + 1234567890”,“从Xam.Messaging.Plugin那里问好”);

Also please add the android.permission.SEND_SMS permission to your Android manifest file 另外,请向您的Android清单文件添加android.permission.SEND_SMS权限

For more details please go through https://github.com/cjlotz/Xamarin.Plugins/blob/master/Messaging/Details.md 有关更多详细信息,请访问https://github.com/cjlotz/Xamarin.Plugins/blob/master/Messaging/Details.md

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM