0

In the following code i am trying to have a text box and a button where the user would input text and on button press i would just send the SMS to the hardcoded value ,But i do not get any exception and i have included the permissions .But when i send the SMS the app closes down the error log doest have anything and i am trying this on a real device not an emulator.Does any body know what might be the problem here

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

            try {
                EditText et =(EditText) findViewById(R.id.editText1);
        final String smstext =et.getText().toString().trim();
                sms.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
    // TODO Auto-generated method stub
    if (smstext != "" || smstext != null)
    {
                SmsManager smsmgr=SmsManager.getDefault();
        smsmgr.sendTextMessage("+xxxxxxxx", null, smstext , null , null);

    }
    else
    {
    Toast.makeText(getApplicationContext(),"Enter Message", Toast.LENGTH_SHORT).show();
    }
}
});

    }
            catch (Exception e) 
            {
                Toast.makeText(getApplicationContext(),"Send SMS error: "+ e, Toast.LENGTH_SHORT).show();
            }

main.xml

  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:id="@+id/linearLayout2" android:layout_width="fill_parent" android:layout_height="fill_parent">
      <EditText android:id="@+id/editText1" android:layout_width="fill_parent" android:layout_height="wrap_content">
         <requestFocus></requestFocus>
      </EditText>
      <Button android:text="Send SMS" android:id="@+id/sms" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
      <Button android:text="Back" android:id="@+id/back1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>


   </LinearLayout>
Rajeev
  • 44,985
  • 76
  • 186
  • 285

1 Answers1

1

In order to get some feedback on the result sendTextMessage(), you should pass an PendingIntent object on the 5th parameter of the function.

A quick online search brought this tutorial: SMS Messaging in Android

Edit: I re-checked the code you pasted, and you should also move the definitions of et and smstext within the onClick method.

Jon RePe
  • 81
  • 6