Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
548 views
in Technique[技术] by (71.8m points)

android - Sending SMS programmatically without opening message app

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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_SMS permission in AndroidManifest.xml to send message

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...