如何在Android应用程序中向选定的ID发送电子邮件?

时间:2016-04-12 04:15:20

标签: android

此方法适用于特定的电子邮件,但我希望它在选择某个用户时动态填充。

任何想法? 这是我的详细活动代码

我有一个列表视图,点击后会显示特定人员的详细信息,我在该详细信息活动中有一个按钮,点击后应该填充特定的电子邮件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)
    {

    }
}*/

}

2 个答案:

答案 0 :(得分:0)

变化:

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

为:

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

答案 1 :(得分:0)

对我有用的是以下代码:

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

它将a_email(textview)中的值提取到进一步传递给

的字符串中
 intent.putExtra(Intent.EXTRA_EMAIL, to);
相关问题