2012-07-20 66 views
0

其實我的概念是android應用程序代碼消息必須發送一個特定的號碼。我的代碼是在AndroidManifest.xml文件需要以編程方式通過Android App向特定號碼發送短信

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

    buttonSend = (Button) findViewById(R.id.buttonSend); 
    textPhoneNo = (EditText) findViewById(R.id.editTextPhoneNo); 
    textSMS = (EditText) findViewById(R.id.editTextSMS); 

    buttonSend.setOnClickListener(new OnClickListener() { 

     public void onClick(View v) { 

      String phoneNo = textPhoneNo.getText().toString(); 
      String sms = textSMS.getText().toString(); 

      try { 
       SmsManager smsManager = SmsManager.getDefault(); 
       smsManager.sendTextMessage(phoneNo, null, sms, null, null); 
       String smsNumber = textPhoneNo.getText().toString(); 
        String smsText = textSMS.getText().toString(); 
        smsManager.sendTextMessage(smsNumber, null, smsText, null, null); 
       Toast.makeText(getApplicationContext(), "SMS Sent!", 
         Toast.LENGTH_LONG).show(); 
      } catch (Exception e) { 
       Toast.makeText(getApplicationContext(), 
         "SMS faild, please try again later!", 
         Toast.LENGTH_LONG).show(); 
       e.printStackTrace(); 
      } 
     } 
    }); 
} 
+1

添加權限到你的manifest文件 – Houcine 2012-07-20 11:57:47

回答

1

權限:SEND_SMS的

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

給一個,但不是爲我工作.. – NagarjunaReddy 2012-07-20 12:00:49

+0

@NagarjunaReddy「不工作」真的很煩人的答案。任何異常(logcat)或其他? – 2012-07-20 12:01:48

+0

沒有例外沒有錯誤我得到吐司消息短信發送,但消息不是給該用戶的單詞。 – NagarjunaReddy 2012-07-20 12:03:24