通过Android App以编程方式将短信发送到特定号码

时间:2012-07-20 11:52:07

标签: android

实际上我的概念是用android应用程序代码消息必须发送一个特定的号码。我的代码是

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 个答案:

答案 0 :(得分:1)

AndroidManifest.xml文件中所需的权限:

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