Another option is to use the built-in ACTION_SEND or its variants, so that you are using the facilities that are already available. This is and especially useful approach for a beginner.
Look for ACTION_SEND in this document: http://developer.android.com/reference/android/content/Intent.html and you can piece together how to use it. As an example, this bit of code lets the user choose whether to send the information as SMS or as an email. --- private void sendData(String value) { // We're going to send the data in two forms, as message text and as an // attachment FileOutputStream fos; if (value == null || !(mSendText.isChecked() || mSendFile.isChecked())) { Toast.makeText(this, R.string.msg_nothing_to_send, Toast.LENGTH_LONG).show(); return; } try { fos = this.openFileOutput("data.csv", Context.MODE_WORLD_READABLE); fos.write(value.getBytes()); fos.close(); Uri fileUri = Uri.fromFile(getFileStreamPath("data.csv")); // Log.d(TAG, "File Uri: " + fileUri.toString()); Intent i = new Intent(Intent.ACTION_SEND); i.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); if (mSendText.isChecked()) i.putExtra(Intent.EXTRA_TEXT, value); if (mSendFile.isChecked()) i.putExtra(Intent.EXTRA_STREAM, fileUri); i.putExtra(Intent.EXTRA_TITLE, "bpdata.csv"); i.putExtra(Intent.EXTRA_SUBJECT, "bpdata.csv"); i.setType("text/plain"); Intent ai = Intent.createChooser(i, getString(R.string.msg_choose_send_method)); ai.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(ai); } catch (FileNotFoundException e) { Log.e(TAG, getString(R.string.title_error)); Toast.makeText(this, getString(R.string.title_error), Toast.LENGTH_LONG).show(); e.printStackTrace(); } catch (IOException e) { Log.e(TAG, getString(R.string.title_error)); Toast.makeText(this, getString(R.string.title_error), Toast.LENGTH_LONG).show(); e.printStackTrace(); } } --- On May 10, 2:20 am, rajesh chandrasekaran <[email protected]> wrote: > Hi all > > I am new in android, i need to send the sms in android, i have tryed > with the following code, and i have tested in emulator, but i am > getting error. > > i need to know how to send sms, and also please tell me below code > will work or not > > import android.app.Activity; > import android.app.AlertDialog; > import android.app.PendingIntent; > import android.content.Intent; > import android.os.Bundle; > import android.telephony.gsm.SmsManager; > import android.view.View; > import android.view.View.OnClickListener; > import android.widget.Button; > import android.widget.EditText; > > public class email extends Activity { > /** Called when the activity is first created. */ > > public EditText name_text; > > @Override > public void onCreate(Bundle savedInstanceState) { > super.onCreate(savedInstanceState); > setContentView(R.layout.email); > > final EditText to_text = (EditText) > findViewById(R.id.to_text); > to_text.setOnClickListener(new OnClickListener() { > public void onClick(View v) { > to_text.setText(""); > } > }); > > final EditText message_label = (EditText) > findViewById(R.id.message_text); > message_label.setOnClickListener(new OnClickListener() { > public void onClick(View v) { > message_label.setText(""); > } > }); > > Button send_button = (Button) findViewById(R.id.send_button); > send_button.setOnClickListener(new OnClickListener() { > > public void onClick(View arg0) { > String tvstring = to_text.getText().toString(); > String bodystring = > message_label.getText().toString(); > tvstring = tvstring.trim(); > > if (tvstring.equalsIgnoreCase("Enter The Email-ID") > || tvstring.equalsIgnoreCase("")) > { > AlertDialog.Builder builder = new > AlertDialog.Builder(email.this); > builder.setMessage("Please Enter Your > Email-ID or Mobile > Number").setPositiveButton( > "OK", null).show(); > } > else if (bodystring.equalsIgnoreCase("Enter The Your > Message")|| bodystring.equalsIgnoreCase("")) > { > AlertDialog.Builder builder = new > AlertDialog.Builder(email.this); > builder.setMessage("Please Enter Your > Message").setPositiveButton( > "OK", null).show(); > }else > { > sendSMS(tvstring, bodystring); > } > } > }); > } > > private void sendSMS(String phoneNumber, String message) > { > PendingIntent pi = PendingIntent.getActivity(this, 0, > new Intent(this, email.class), 0); > SmsManager sms = SmsManager.getDefault(); > sms.sendTextMessage(phoneNumber, null, message, pi, > null); > } > > } > > -- > You received this message because you are subscribed to the Google > Groups "Android Developers" group. > To post to this group, send email to [email protected] > To unsubscribe from this group, send email to > [email protected] > For more options, visit this group > athttp://groups.google.com/group/android-developers?hl=en -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/android-developers?hl=en

