编程发送短信,而无需打开应用程序的消息发送短信、应用程序、消息

2023-09-12 05:17:47 作者:秘密乐趣.

到目前为止,我现在用的是下面的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/>