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

Intent を起動するために AsyncTask を開始する必要はありません。クイック フィックスは、doInBackground コードを preExecute または postExecute に貼り付けることができます。

それが機能するかどうか教えてください:)

于 2013-07-16T06:54:26.030 に答える