2012-04-29 95 views
0

我的應用程序有一個圖像按鈕,點擊時應通過短信發送。我如何讓我的應用程序做到這一點?請幫忙。僅通過短信分享

我還希望用戶能夠從他的設備上的聯繫人列表中選擇一個聯繫人。

Jumana

+1

爲什麼你想限制選項只有短信?分享意圖的力量是讓用戶自由選擇。 – Eloff 2012-04-29 09:30:15

回答

16

要使用意圖發送短信,請使用此代碼:

String smsBody="Sms Body"; 
Intent sendIntent = new Intent(Intent.ACTION_VIEW); 
sendIntent.putExtra("sms_body", smsBody); 
sendIntent.setType("vnd.android-dir/mms-sms"); 
startActivity(sendIntent); 

我希望這能幫到你!

+1

在kitkat上找不到處理意圖的活動 – 2013-12-30 11:54:37

+0

看起來KitKat已經改變了SMS API。我沒有嘗試新的API。 – 2013-12-30 14:48:19

+5

http://stackoverflow.com/questions/20079047/android-kitkat-4-4-hangouts-cannot-handle-sending-sms-intent/20079048#20079048 – 2013-12-30 14:54:14

-2

試試下面的代碼...在按鈕的onClickListener()添加該代碼。

Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND); 
     sharingIntent.setType("text/plain"); 
     String shareBody = "Here is the share content body"; 
     sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject Here"); 
     sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody); 
     startActivity(Intent.createChooser(sharingIntent, "Share via")); 
+0

但是這顯示所有可用的選項。我只需要選擇器的「消息」選項。 – Jumana 2012-04-29 14:31:02

+0

此代碼將顯示所有選項共享不是由Nejab提供的短信solutin是你想要的 – 2015-02-12 08:40:45

1

步驟來使發送短信:

1-在機器人艙單,添加發送短信權限如下:

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

2-在YOUT活動添加此方法:

public void sendSms(final String receiverNumber, final String smsBody) { 
    SmsManager smsManager = SmsManager.getDefault(); 
    smsManager.sendTextMessage(receiverNumber, null, smsBody, null, null);   
} 
+0

這將不會顯示已經在設備上的聯繫人列表嗎? – Jumana 2012-04-29 14:33:02

+0

不,這段代碼會以編程方式發送短信。如果你想使用默認的短信程序,請使用Intents作爲@sandip armal的回答。 – 2012-04-29 14:45:09

+0

謝謝@Ali Behzadian Nejad。但我希望用戶能夠選擇僅通過短信共享。意圖選擇器顯示Twitter,Facebook等?用戶還應該能夠從他的聯繫人列表中選擇一個聯繫人來發送短信。 – Jumana 2012-04-29 14:52:36