Jump to: navigation, search

Teams Winter 2011/team1/Android/Send Email

2,089 bytes added, 17:25, 7 April 2011
no edit summary
9.1.5 Now it is time to build your project so that our id's will be generated.
=== 9.2 Creating === 9.2.1 Lets add our private variables and that we get the right layout once it has been started.<source lang="java">private EditText sEmailTo; private EditText sSubject; private EditText sBody; private Button btnSend; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //setContentView(; setContentView(;</source> 9.2.2 If we capture the intent now we will be able to retrieve the saved email address of the selected student.<source lang="java">// get the intent sent to this class Intent i = this.getIntent(); sEmailTo = (EditText) findViewById(; //sEmailTo.setText(i.getStringExtra("oldEmail")); sSubject = (EditText) findViewById(; sBody = (EditText) findViewById(;</source> 9.2.3 In order to have our application send the email we must implement a button.<source lang="java">btnSend.setOnClickListener(new OnClickListener() { public void onClick(View v) { if (v.getId() == { finish(); } } }); }</source> 9.2.4 We must now override the finish() method and send the email.<source lang="java">@Override public void finish() { // Setup the recipient in a String array String[] mailto = { sEmailTo.getText().toString() }; // Create a new Intent to send messages Intent sendIntent = new Intent(Intent.ACTION_SEND); // Add attributes to the intent sendIntent.putExtra(Intent.EXTRA_EMAIL, mailto); sendIntent.putExtra(Intent.EXTRA_SUBJECT, sSubject.getText().toString()); sendIntent.putExtra(Intent.EXTRA_TEXT, sBody.getText().toString()); sendIntent.setType("text/plain"); startActivity(Intent.createChooser(sendIntent, "SendMail"));  super.finish(); }</source>

Navigation menu