I am making a simple android application to send message to a mobile network. I am using the following code but when I enter the Phone number and the Text Message and Click the Send Button it does not sends the Message and says Message Sending Failed.
My Main Activity "Main.java" is this..
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.telephony.SmsManager;
import android.widget.Toast;
public class Main extends ActionBarActivity {
Button Btn;
EditText textphoneNo;
EditText Message;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
Btn = (Button) findViewById(R.id.SendSMS);
textphoneNo = (EditText) findViewById(R.id.TextPhoneNo);
Message = (EditText) findViewById(R.id.TextSMS);
Btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
sendSMSMessage();
}
protected void sendSMSMessage() {
Log.i("Send SMS", "");
String phoneNo = textphoneNo.getText().toString();
String message = Message.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 Sending Failed.",
Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
@SuppressWarnings("unused")
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
});
}
}
When the send button is clicked the control goes to the catch statement and message is not sent, please tell me that what is the problem here which is not letting the control to go to the try statement.