2014-01-15 29 views
1

所以我試圖讓我的應用程序在用戶按下按鈕時自動發送短信給指定的號碼。Android - 通過點擊按鈕發送短信。

我可以讓它打開信使和寫文字,但我不能讓它自動發送。

我的代碼如下(我認爲重要的部分);

@Override 
public void onClick(View a) { 


    if(a.equals(sms)){ 
     tekst = (TextView) findViewById(R.id.txt); 
     Uri tlf = Uri.parse("smsto:"+tekst.getText().toString()); 
     Intent c = new Intent(Intent.ACTION_VIEW, tlf); 
     c.setData(tlf); 
     c.putExtra("sms_body","Hjelp jeg er i fare!"); 
     startActivity(c); 



    }else{ 
     tekst = (TextView) findViewById(R.id.txt); 
     Intent c = new Intent(Intent.ACTION_CALL); 
     Uri tlf = Uri.parse("tel:"+tekst.getText().toString()); 
     c.setData(tlf); 
     startActivity(c); 


    } 

} 

那麼,我該如何讓它發送短信?

BTW,我已經添加了權限:「android.permission.SEND_SMS」

+1

你應該以'SmsManager'接口另一種方法(http://developer.android.com/reference/android/telephony/SmsManager.html),而不是創建年代由默認的短信應用所消耗的意圖。 – Estel

回答

0

試試這個:

String phoneNumber = "<phone_number_here>"; 
String message = "Test Message"; 
SmsManager smsManager = SmsManager.getDefault(); 
smsManager.sendTextMessage(phoneNumber, null, message, null, null); 

請注意,這是一個非常簡單的剪斷的代碼,你可以實現更多。 如果你想看到的短信顯示在任何其他SMS客戶端的上/應用安裝宥必須使用:

ContentValues values = new ContentValues(); 
values.put("address", "<phone_number_here>"); 
values.put("body", "Test Message"); 
getContentResolver().insert(Uri.parse("content://sms/sent"), values); 

,並添加:

<uses-permission android:name="android.permission.WRITE_SMS" /> 
<uses-permission android:name="android.permission.READ_SMS" /> 
0

試試這個

try { 
     SmsManager smsManager = SmsManager.getDefault(); 
     smsManager.sendTextMessage([number], null, [sms], null, null); 
     Toast.makeText(getApplicationContext(), "SMS Sent!", 
      Toast.LENGTH_SHORT).show(); 
    } catch (Exception e) { 
     Toast.makeText(getApplicationContext(), 
      "SMS faild, please try again later!", 
        Toast.LENGTH_SHORT).show(); 
     e.printStackTrace(); 

    } 

哪裏[號碼]是你想要發送短信的號碼,[短信]是你想要發送的短信

0

試試這個。

//Declare the button and the tetviews to input number and the message 
Button sendSMSBtnBtn = (Button) findViewById(R.id.btnSendSMS); 
txtphoneNo = (EditText) findViewById(R.id.editTextPhoneNo); 
txtMessage = (EditText) findViewById(R.id.editTextSMS); 

//Calling the method sendSMSMessage in the button click event 
sendSMSBtn.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View view) { 
      sendSMSMessage(); 
     } 
});} 

// Method to send SMS using SMS Manager 
protected void sendSMSMessage() { 
     Log.i("Send SMS", ""); 

     String phoneNo = txtphoneNo.getText().toString(); 
     String message = txtMessage.getText().toString(); 

     try { 
     SmsManager smsManager = SmsManager.getDefault(); 
     smsManager.sendTextMessage(phoneNo, null, message, null, null); 
     Toast.makeText(getApplicationContext(), "SMS sent.", 
     Toast.LENGTH_LONG).show(); 
     } catch (Exception e) { 
     Toast.makeText(getApplicationContext(), 
     "SMS faild, please try again.", 
     Toast.LENGTH_LONG).show(); 
     e.printStackTrace(); 
     } 
    } 

注意:確保在Manifest文件中設置了以下權限。

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