2014-02-15 29 views
0

嗨,大家好我是新手編程。下面的代碼可以通過按下按鈕將預定義的SMS定義爲預定義的號碼,但它會帶給消息編輯器屏幕,並要求我點擊發送。我如何直接發送短信,而無需轉到消息編輯器。我發現很少有人問這個問題,其中一個解決方案是使用SMSmanger,但我不知道如何將SMSmanger代碼用於我的代碼。如何使用SMS SmsManager

public class SendSMSActivity extends Activity { 

Button buttonSend;  
Button buttonSend2; 


@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main);  

    buttonSend = (Button) findViewById(R.id.buttonSend);  
    buttonSend2 = (Button) findViewById(R.id.buttonSend2); 



    OnClickListener listener = new OnClickListener() {   

     @Override 
     public void onClick(View v) { 

      switch (v.getId()) {  

      case R.id.buttonSend:  
       Intent sendIntent = new Intent(Intent.ACTION_VIEW); 
       sendIntent.putExtra("sms_body", "#abc"); 
       sendIntent.putExtra("address", "9900990"); 
       sendIntent.setType("vnd.android-dir/mms-sms"); 
       startActivity(sendIntent);  
       break; 

      case R.id.buttonSend2: 
       Intent sendIntent1 = new Intent(Intent.ACTION_VIEW); 
       sendIntent1.putExtra("sms_body", "#def"); 
       sendIntent1.putExtra("address", "9900990"); 
       sendIntent1.setType("vnd.android-dir/mms-sms"); 
       startActivity(sendIntent1); 
       break; 

      } 

     } 
    }; 

buttonSend.setOnClickListener(listener);  
buttonSend2.setOnClickListener(listener); 
} 
) 

謝謝

+0

你嘗試過谷歌? 「如何以編程方式發送短信Android」。 – Sajmon

+0

[Android在按鈕點擊時自動發送短信]的可能重複(http://stackoverflow.com/questions/10607361/android-send-sms-automatically-on-button-click) –

+0

謝謝。 「Android按鈕點擊自動發送短信」答案幫助了我。 – user3296057

回答

0

嘗試以下操作:

SmsManager sms = SmsManager.getDefault(); 
sms.sendTextMessage(phoneNumber, null, message, null, null); 

注:其將需要獲得許可android.permission.SEND_SMS