2012-03-21 45 views
43

我想通過故意發送短信,但是當我使用此代碼,它重定向我錯了聯繫人發送短信:通過意向

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

爲什麼?另外,我知道一路跟隨短信發送,但我不知道如何代碼如下:

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

其中XXXXXXXXXXXX是電話號碼。

+0

要讀出機器人的源代碼,也SmsManager。 – JoxTraex 2012-03-21 04:55:11

+0

也可以得到'ActivityNotFoundException:找不到處理Intent(「vnd.android-dir/mms-sms」)的活動。最好不要使用這種方法。 – astuter 2014-10-02 11:30:11

回答

58

我從一個Blog開發了這個功能。有兩種方法可以發送短信。

  1. 打開本地短信作曲家
  2. 寫你的消息,並從你的android應用

這是第一個方法的代碼發送。

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> 

活動

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); 
     } */ 
} 

注: - 在這種方法中,你並不需要在AndroidManifest.xml文件裏面SEND_SMS權限。

對於第二種方法,請參閱BLOG。你會從這裏找到很好的解釋。

希望這將幫助你...

+2

還有一件事,爲了測試這個應用程序的目的,你可以打開2個仿真器;他們有像5554&5555或類似的東西。你用這個數字來測試它。 – Prem 2012-03-21 05:05:00

+0

這是從所有設備和Android版本通過意向發送短信的唯一工作方式。 – astuter 2014-10-02 11:27:21

+0

@ I-droid:你指的是哪一種方法? – 2014-10-03 19:31:19

5

試試看看這個代碼。它會工作

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

希望這會幫助你。

-4

試試這個code.It將工作

Intent intent = new Intent(this, sendSMS.class); 
    intent.putExtra("key_1",values(String SMS meg)); 
    intent.putExtra("key_2",values(String mobile No)); 
    startActivity(intent); 

    sendSMS Class in 
     { 
      String msg = getIntent().getStringExtra("key_1", 0) 
      String no = getIntent().getStringExtra("key_2", 0) 
      // String no = "5556"; 

      PendingIntent SendPI = PendingIntent.getBroadcast(this,0, new Intent(), 0); 
      SmsManager smsManager = SmsManager.getDefault();      
      smsManager.sendTextMessage(no,null,msg, SendPI, null); 
     } 

to try to send sms this way 
+4

這個答案很奇怪。 – Roel 2015-07-24 11:31:30

38
Uri uri = Uri.parse("smsto:0800000123"); 
Intent it = new Intent(Intent.ACTION_SENDTO, uri); 
it.putExtra("sms_body", "The SMS text"); 
startActivity(it); 
+2

這不適用於Android 4.0.3(更好?)。我必須改用Intent.EXTRA_TEXT。任何想法爲什麼? – wojciii 2014-09-20 18:02:22

+1

剛剛在一些設備上測試了這個,運行Android 6.0.1和4.4.4:在所有情況下,「sms_body」工作正常,「Intent.EXTRA_TEXT」不起作用。 – Jonik 2016-08-11 12:24:14

2

希望這是工作,這是工作在我的應用程序

SmsManager.getDefault().sendTextMessage("Phone Number", null, "Message", null, null); 
+0

這種方式需要特權嗎? – Radon8472 2015-06-02 22:31:17

+0

來自文檔:_使用此方法要求您的應用具有SEND_SMS權限._ – shkschneider 2015-07-17 12:59:43

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

希望對某人有幫助

+0

是的!謝謝:)它幫助了我。 – 1111161171159459134 2015-08-30 21:10:41

+0

謝謝。那是我一直在尋找的 – Sam 2017-04-23 13:42:31

0

這是另一種解決方案使用SMSManager:

SmsManager smsManager = SmsManager.getDefault(); 
smsManager.sendTextMessage("PhoneNumber-example:+989147375410", null, "SMS Message Body", null, null);