Android - 按下按钮发送短信。

时间:2014-01-15 15:59:46

标签: android onclick sms

因此,当用户按下按钮时,我正在尝试让我的应用程序自动发送短信到指定的号码。

我可以打开信使并写下文字,但我不能让它自动发送。

我的代码如下(我猜的重要部分);

@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);


    }

}

那么,我怎样才能让它发送短信?

是的,我已经添加了权限:“android.permission.SEND_SMS”

3 个答案:

答案 0 :(得分: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" />

答案 1 :(得分: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();

  }

其中[number]是您要发送短信的号码,[短信]是您要发送的短信

答案 2 :(得分: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" />
相关问题