Android : android SmsManager project not workin

on Monday, November 3, 2014


i have this code (mainpackage.java) for sending a sms to a number but it always does not workin ! onClick runs when u click on send button then try block runs but failed to sending sms and toast says "SMS faild, please try again later!" , what is wrong ???



package com.behnam.temp3.mainpackage;
import android.app.Activity;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {

EditText txt_number;
EditText txt_text;
Button btn_send;


@Override
public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.main);

txt_number = (EditText) findViewById(R.id.editTextPhoneNo);
txt_text = (EditText) findViewById(R.id.editTextSMS);
btn_send = (Button) findViewById(R.id.buttonSend);
btn_send.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {

String phoneNo = txt_number.getText().toString();
String sms = txt_text.getText().toString();
try {
SmsManager smsmanager = SmsManager.getDefault();
smsmanager.sendTextMessage(phoneNo, null, sms, null, null);
Toast.makeText(getApplicationContext(), "SMS Sent", 1).show();

}
catch (Exception e) {
Toast.makeText(getApplicationContext(),"SMS faild, please try again later!",Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
});
}


}


0 comments:

Post a Comment