Android 在不打开消息应用程序的情况下以编程方式发送短信

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26311243/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:15:00  来源:igfitidea点击:

Sending SMS programmatically without opening message app

androidandroid-activity

提问by SoulRayder

So far I am using the following code to send SMS to another phone through my app.

到目前为止,我正在使用以下代码通过我的应用程序向另一部手机发送短信。

Intent intent = new Intent( Intent.ACTION_VIEW, Uri.parse( "sms:" + srcNumber)); 
                    intent.putExtra( "sms_body", message ); 
                    startActivity(intent);

However, this opens up the native messaging app, thereby putting my app's activity in the background. Is it possible to send the SMS directlywithout the native messaging app opening up? If yes, how?

但是,这会打开本机消息传递应用程序,从而将我的应用程序的活动置于后台。是否可以在不打开本机消息传递应用程序的情况下直接发送短信?如果是,如何?

回答by Himanshu Agarwal

You can send message from your application throug this:

您可以通过以下方式从您的应用程序发送消息:

public void sendSMS(String phoneNo, String msg) {
    try {      
        SmsManager smsManager = SmsManager.getDefault();
        smsManager.sendTextMessage(phoneNo, null, msg, null, null);    
        Toast.makeText(getApplicationContext(), "Message Sent",
              Toast.LENGTH_LONG).show();
    } catch (Exception ex) {
        Toast.makeText(getApplicationContext(),ex.getMessage().toString(),
              Toast.LENGTH_LONG).show();
        ex.printStackTrace();
    } 
}

Also you need to give SEND_SMSpermission in AndroidManifest.xmlto send message

您还需要授予发送消息的SEND_SMS权限AndroidManifest.xml

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

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

回答by Abhishek Chaubey

public void sendLongSMS() {
    String phoneNumber = "0123456789";
    String message = "Hello World! Now we are going to demonstrate " + 
        "how to send a message with more than 160 characters from your Android application.";
    SmsManager smsManager = SmsManager.getDefault();
    ArrayList<String> parts = smsManager.divideMessage(message); 
    smsManager.sendMultipartTextMessage(phoneNumber, null, parts, null, null);
}

and don't forget to add

并且不要忘记添加

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

回答by Sujay U N

Sending sms with permission request :

发送带有权限请求的短信:

Add In manifest :

添加清单:

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

Add Java Function :

添加 Java 函数:

void sendSmsMsgFnc(String mblNumVar, String smsMsgVar)
{
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.SEND_SMS) == PackageManager.PERMISSION_GRANTED)
    {
        try
        {
            SmsManager smsMgrVar = SmsManager.getDefault();
            smsMgrVar.sendTextMessage(mblNumVar, null, smsMsgVar, null, null);
            Toast.makeText(getApplicationContext(), "Message Sent",
                    Toast.LENGTH_LONG).show();
        }
        catch (Exception ErrVar)
        {
            Toast.makeText(getApplicationContext(),ErrVar.getMessage().toString(),
                    Toast.LENGTH_LONG).show();
            ErrVar.printStackTrace();
        }
    }
    else
    {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
        {
            requestPermissions(new String[]{Manifest.permission.SEND_SMS}, 10);
        }
    }

}

回答by SoulRayder

Yes, found the answer to my own question :)

是的,找到了我自己问题的答案:)

Use the following code for the same :

使用以下代码相同:

 SmsManager sms = SmsManager.getDefault();
                     sms.sendTextMessage(srcNumber, null, message, null, null);

This requires the following permission to be declared on the android manifest xml.

这需要在 android manifest xml 上声明以下权限。

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

回答by Dilavar Malek

SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo, null, sms, null, null);