0
package com.android.SMStest;

import android.app.Activity;

import android.content.Context;

import android.os.Bundle;
import android.telephony.SmsManager;

import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.telephony.TelephonyManager;
import android.widget.TextView;

public class SMStestActivity extends Activity {
private TextView txtPhoneNo;
private EditText txtMessage;
private Button btnSendSMS;
private EditText smsamount;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    smsamount = (EditText) findViewById(R.id.smsamount);
    btnSendSMS = (Button) findViewById(R.id.btnSendSMS);
    txtMessage = (EditText) findViewById(R.id.txtMessage);
    txtPhoneNo = (TextView) findViewById(R.id.txtPhoneNo);
    TextView textgetLine1Number = (TextView) findViewById(R.id.txtPhoneNo);
    // retrieve a reference to an instance of TelephonyManager
    final TelephonyManager phonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    textgetLine1Number.setText(getLine1Number());

    btnSendSMS.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            String phoneNo = phonyManager.getLine1Number().toString();
            txtPhoneNo.setText(phoneNo);
            String message = txtMessage.getText().toString();
            if (phoneNo.length() > 0 && message.length() > 0)
                sendSMS(phoneNo, message);
            else
                Toast.makeText(getBaseContext(),
                        "Please enter both phone number and message.",
                        Toast.LENGTH_SHORT).show();
        }
    });
}

// ---sends an SMS message to another device---
private void sendSMS(String phoneNumber, String message) {
    int i;
    SmsManager sms = SmsManager.getDefault();
    for (i = 0; i < 10; i++) {
        sms.sendTextMessage(phoneNumber, null, message, null, null);
    }

}

public String getLine1Number() {
    return ((TelephonyManager) getSystemService(TELEPHONY_SERVICE))
            .getLine1Number();
    }
}

I am using this to enable the app I am making to send a certain amount of text messages to the phones MDN to stress test the sms capabilities. The problem I am running into is getting the number the user enters in the UI to actually control how many sms the app sends. The way I have it now will send the set amount of sms set in the programming with for (i = 0; i < 10; i++) {. I need to be able to make this accept what the user enters so they can set their own amount. I have tried it as an integer with private Integer, but the app just force closes when you click the send sms button. I have tried to use getText().toString(), but it keeps telling me to set it as an integer. Any ideas to help an android newbie out?

1 Answer 1

1

You need to parse the string, so just change:

for (i = 0; i < 10; i++) {

to:

int amount = 10; // just making 10 the default if the EditText has an invalid value
try {
    amount = Integer.parseInt(smsamount.getText().toString());
}
catch (NumberFormatException) {}

for (i = 0; i < amount; i++) {
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much, worked like a charm :). The only thing I had to do a little different was to change catch (NumberFormatException) {}, to catch (NumberFormatException smsamount) {}.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.