1

我正在向朋友发送邮件,但以下代码仅发布一次。之后,我就不能再发邮件了。请指导我。

package com.mkyong.android;

    import android.app.Activity;
    import android.content.Intent;
    import android.os.AsyncTask;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.Toast;

    public class SendEmailActivity extends Activity {

        Button buttonSend;

        ConnectionDetector cd;
        Boolean isInternetPresent = false;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            buttonSend = (Button) findViewById(R.id.buttonSend);

            buttonSend.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {

                    cd = new ConnectionDetector(getApplicationContext());

                    isInternetPresent = cd.isConnectingToInternet();
                    if (isInternetPresent) {
                        new MyTask().execute();
                    } else {
                        Toast.makeText(getApplicationContext(), "Not Enabled",
                                Toast.LENGTH_LONG).show();
                    }
                }
            });
        }

        class MyTask extends AsyncTask<String, String, String> {

            @Override
            protected String doInBackground(String... params) {
                Intent i = new Intent(Intent.ACTION_SEND);

                i.putExtra(android.content.Intent.EXTRA_EMAIL,
                        new String[] { "Xnn123@gmail.com" });
                i.putExtra(android.content.Intent.EXTRA_SUBJECT, "subject of email");
                i.putExtra(android.content.Intent.EXTRA_TEXT, "body of email");
                i.setType("message/rfc822");
                try {
                    startActivity(Intent.createChooser(i, "Send mail..."));
                } catch (android.content.ActivityNotFoundException ex) {
                    Toast.makeText(getApplicationContext(),
                            "There are no email clients installed.",
                            Toast.LENGTH_SHORT).show();
                }
                return null;
            }

            @Override
            protected void onPostExecute(String result) {

            }
        }
    }
4

1 回答 1

0

您无需启动 AsyncTask 即可触发 Intent,可以将您的 doInBackground 代码粘贴到 preExecute 或 postExecute 中。

让我知道它是否有效:)

于 2013-07-16T06:54:26.030 回答