2012-07-20 65 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