在不打开消息应用程序的情况下以编程方式发送短信 [英] Sending SMS programmatically without opening message app

查看:21
本文介绍了在不打开消息应用程序的情况下以编程方式发送短信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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);

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

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

推荐答案

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

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();
    } 
}

你还需要在AndroidManifest.xml中给SEND_SMS权限发送消息

Also you need to give SEND_SMS permission in AndroidManifest.xml to send message

这篇关于在不打开消息应用程序的情况下以编程方式发送短信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆