编程发送短信,而无需打开应用程序的消息 [英] Sending SMS programmatically without opening message app

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

问题描述

到目前为止,我现在用的是下面的code到我的应用程序发送短信到另一部手机。

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

此外,你需要给 SEND_SMS 许可的Andr​​oidManifest.xml 发送邮件

<使用-权限的Andr​​oid:名称=android.permission.SEND_SMS/>

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

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