简体   繁体   中英

How to send email to a selected id in android app?

This method is working for a specific email but I want it to dynamically populate when a certain user is selected.

Any ideas?? This is my code for details activity

I have a listview which on click displays details of a particular person and I have a button in that details activity which after clicking should populate the particular email id.

public class AgentDetails extends AppCompatActivity {

private TextView a_id;
private TextView a_fname;
private TextView a_lname;
private TextView a_phone;
private TextView a_email;
private TextView a_position;
private ImageButton sendEmail;

private String JSON_STRING;

private String id;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_agent_details);


    Intent intent = getIntent();

    id = intent.getStringExtra(Config.AGT_ID);

    a_id = (TextView) findViewById(R.id.a_id);
    a_fname = (TextView) findViewById(R.id.a_fname);
    a_lname = (TextView) findViewById(R.id.a_lname);
    a_phone = (TextView) findViewById(R.id.a_phone);
    a_email = (TextView) findViewById(R.id.a_email);
    a_position = (TextView) findViewById(R.id.a_position);
    sendEmail = (ImageButton) findViewById(R.id.sendEmail);


    a_id.setText(id);
    a_fname.setText(intent.getStringExtra(Config.TAG_FNAME));
    a_lname.setText(intent.getStringExtra(Config.TAG_LNAME));
    a_phone.setText(intent.getStringExtra(Config.TAG_PHONE));
    a_email.setText(intent.getStringExtra(Config.TAG_EMAIL));
    a_position.setText(intent.getStringExtra(Config.TAG_POSITION));


    getAgent();
}

public void process(View view)
{

    Intent intent = null, chooser = null;

        if(view.getId()== R.id.sendEmail)
        {

            intent = new Intent(intent.ACTION_SEND);
            intent.setData(Uri.parse("mailto:"));
            String[] to = {intent.setText(intent.getStringExtra(Config.TAG_EMAIL))};

            intent.putExtra(Intent.EXTRA_EMAIL, to);
            intent.putExtra(Intent.EXTRA_SUBJECT, "Booking Request");
            intent.putExtra(Intent.EXTRA_TEXT, "Need more information about booking");
            intent.setType("message/rfc822");//need for MIME message type
            chooser = Intent.createChooser(intent, "Send Email"); //selects the in-build email app
            startActivity(chooser);
        }

    }




private void getAgent(){
    class GetAgent extends AsyncTask<Void,Void,String>{
        ProgressDialog loading;
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            loading = ProgressDialog.show(AgentDetails.this,"Fetching...","Wait...",false,false);
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            loading.dismiss();
            showAgent(s);
        }

        @Override
        protected String doInBackground(Void... params) {
            RequestHandler rh = new RequestHandler();
            String s = rh.sendGetRequestParam(Config.URL_GET_AGT,id);
            return s;
        }
    }
    GetAgent ga = new GetAgent();
    ga.execute();
}

private void showAgent(String json){
    try {
        JSONObject jsonObject = new JSONObject(json);
        JSONArray result = jsonObject.getJSONArray(Config.TAG_JSON_ARRAY);
        JSONObject c = result.getJSONObject(0);
        String fname = c.getString(Config.TAG_FNAME);
        String lname = c.getString(Config.TAG_LNAME);
        String phone = c.getString(Config.TAG_PHONE);
        String email = c.getString(Config.TAG_EMAIL);
        String position = c.getString(Config.TAG_POSITION);


        a_fname.setText(fname);
        a_lname.setText(lname);
        a_phone.setText(phone);
        a_email.setText(email);
        a_position.setText(position);



    } catch (JSONException e) {
        e.printStackTrace();
    }
}

/*@Override
public void onClick(View v) {
    if(v == sendEmail)
    {

    }
}*/

}

Change:

String[] to = {intent.setText(intent.getStringExtra(Config.TAG_EMAIL))};

to:

String to = getIntent().getStringExtra(Config.TAG_EMAIL);

What worked for me is the following code:

String[] to = {a_email.getText().toString()};

it fetches the value from the a_email(textview) into the string which is further passed to

 intent.putExtra(Intent.EXTRA_EMAIL, to);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM