web-dev-qa-db-fra.com

Envoyer SMS via l'intention

Je souhaite envoyer un SMS via l'intention, mais lorsque j'utilise ce code, il me renvoie à un mauvais contact:

Intent intentt = new Intent(Intent.ACTION_VIEW);         
intentt.setData(Uri.parse("sms:"));
intentt.setType("vnd.Android-dir/mms-sms");
intentt.putExtra(Intent.EXTRA_TEXT, "");
intentt.putExtra("address",  phone number);
context.startActivity(intentt);

Pourquoi? De plus, je connais un moyen de suivre l'envoi de SMS, mais je ne sais pas comment coder ceci:

Starting activity: Intent { 
   act=Android.intent.action.SENDTO dat=smsto:%2B**XXXXXXXXXXXX** flg=0x14000000    
   cmp=com.Android.mms/.ui.ComposeMessageActivity }

où XXXXXXXXXXXX est le numéro de téléphone.

54
Ata

J'ai développé cette fonctionnalité à partir d'un blog. Vous pouvez envoyer des SMS de deux manières.

  1. Ouvrir le compositeur natif SMS
  2. écrivez votre message et envoyez depuis votre application Android

Ceci est le code de la 1ère méthode.

Main.xml

<?xml version="1.0" encoding="utf-8"?>  
    <RelativeLayout  
        Android:id="@+id/relativeLayout1"  
        Android:layout_width="fill_parent"  
        Android:layout_height="fill_parent"  
        xmlns:Android="http://schemas.Android.com/apk/res/Android">  

            <Button  
                Android:id="@+id/btnSendSMS"  
               Android:layout_height="wrap_content"  
               Android:layout_width="wrap_content"  
               Android:text="Send SMS"  
               Android:layout_centerInParent="true"  
               Android:onClick="sendSMS">  
           </Button>  
   </RelativeLayout>

Activité

public class SendSMSActivity extends Activity {  
     /** Called when the activity is first created. */  
     @Override  
     public void onCreate(Bundle savedInstanceState) {  
         super.onCreate(savedInstanceState);  
         setContentView(R.layout.main);  
      }  

     public void sendSMS(View v)  
     {  
         String number = "12346556";  // The number on which you want to send SMS  
         startActivity(new Intent(Intent.ACTION_VIEW, Uri.fromParts("sms", number, null)));  
     }  
    /* or 
     public void sendSMS(View v) 
      { 
     Uri uri = Uri.parse("smsto:12346556"); 
         Intent it = new Intent(Intent.ACTION_SENDTO, uri); 
         it.putExtra("sms_body", "Here you can set the SMS text to be sent"); 
         startActivity(it); 
      } */  
 }

REMARQUE: - Dans cette méthode, vous n'avez pas besoin de l'autorisation SEND_SMS dans le fichier AndroidManifest.xml.

Pour la 2ème méthode, reportez-vous à ceci BLOG . Vous trouverez une bonne explication à partir d'ici.

J'espère que ceci vous aidera...

68
Prem
Uri uri = Uri.parse("smsto:YOUR_SMS_NUMBER");   
Intent intent = new Intent(Intent.ACTION_SENDTO, uri);   
intent.putExtra("sms_body", "The SMS text");   
startActivity(intent);  
46
Bao Le
Intent smsIntent = new Intent(Android.content.Intent.ACTION_VIEW);
smsIntent.setType("vnd.Android-dir/mms-sms");
smsIntent.putExtra("address","your desired phoneNumber");         
smsIntent.putExtra("sms_body","your desired message");
startActivity(smsIntent);

J'espère que cette aide à quelqu'un

28
Shan Xeeshi

Essayez ce code. Ça va marcher

Uri smsUri = Uri.parse("tel:123456");
Intent intent = new Intent(Intent.ACTION_VIEW, smsUri);
intent.putExtra("sms_body", "sms text");
intent.setType("vnd.Android-dir/mms-sms"); 
startActivity(intent);

J'espère que ceci vous aidera.

6
Vipul Shah

J'espère que c'est du travail, cela fonctionne dans mon application

SmsManager.getDefault().sendTextMessage("Phone Number", null, "Message", null, null);
3
Dwivedi Ji

Voici une autre solution utilisant SMSManager:

SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage("PhoneNumber-example:+989147375410", null, "SMS Message Body", null, null);
1
Hossein Amini
   Uri uriSms = Uri.parse("smsto:1234567899");   
   Intent intentSMS = new Intent(Intent.ACTION_SENDTO, uriSms);   
   intentSMS.putExtra("sms_body", "The SMS text");   
   startActivity(intentSMS); 
1
bhoomika
/**
 * Intent to Send SMS
 * 
 *
 * Extras:
 *
 * "subject"
 *      A string for the message subject (usually for MMS only).
 * "sms_body"
 *      A string for the text message.
 *  EXTRA_STREAM
 *      A Uri pointing to the image or video to attach.
 *
 *  For More Info:
 *  https://developer.Android.com/guide/components/intents-common#SendMessage
 *
 * @param phoneNumber on which SMS to send
 * @param message text Message to send with SMS
 */
public void startSMSIntent(String phoneNumber, String message) {
    Intent intent = new Intent(Intent.ACTION_SENDTO);
    // This ensures only SMS apps respond
    intent.setData(Uri.parse("smsto:"+phoneNumber));
    intent.putExtra("sms_body", message);
    if (intent.resolveActivity(getPackageManager()) != null) {
        startActivity(intent);
    }
}
0
Dhaval Patel

Si vous voulez un certain message, utilisez ceci:

String phoneNo = "";//The phone number you want to text
String sms= "";//The message you want to text to the phone

Intent smsIntent = new Intent(Intent.ACTION_VIEW, Uri.fromParts("sms", phoneNo, null));
smsIntent.putExtra("sms_body",sms);
startActivity(smsIntent);
0
theeman05
  1. Autorisation manifeste (vous pouvez la mettre après ou avant "application")
 uses-permission Android:name="Android.permission.SEND_SMS"/>
  1. faites un bouton par exemple et écrivez le code ci-dessous (comme précédemment écrit par Prem sur ce fil) et remplacez le numéro de téléphone ci-dessous par un nombre réel, cela fonctionnera:
startActivity(new Intent(Intent.ACTION_VIEW, Uri.fromParts("sms", "phone_Number", null)));
0
Kerelos