如何使用意图打开电子邮件应用程序

时间:2014-03-08 03:14:13

标签: java android android-intent onclick

你是Android开发的新手,遇到了一个问题。我正在讨论来自http://www.youtube.com/watch?v=Sqk154QSe8Y#t=158(新波士顿)的教程视频,据我所知,我有完全相同的代码,但由于某种原因,我的代码无法在按下按钮时打开电子邮件应用程序意图的召唤

Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, emailaddress);
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "I hate you");
emailIntent.setType("plain/type");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, message);

该按钮确实有效,因为我能够更改文本颜色,但电子邮件意图无法打开

我有些事我做错了吗?

这是完整的代码---

    包com.infitenothing.word;

import com.infitenothing.dogcat.R;

import android.R.anim;
import android.R.layout;
import android.app.Activity;
import android.content.Intent;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Layout;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class Email extends Activity implements View.OnClickListener {

EditText personsEmail, intro, personsName, stupidThings, hatefulAction,
        outro;
String emailAdd, beginning, name, stupidAction, hatefulAct, out;
Button sendEmail;
TextView color;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.email);
    initializeVars();
    sendEmail.setOnClickListener(this);
}

private void initializeVars() {
    // TODO Auto-generated method stub
    personsEmail = (EditText) findViewById(R.id.etEmails);
    intro = (EditText) findViewById(R.id.etIntro);
    personsName = (EditText) findViewById(R.id.etName);
    stupidThings = (EditText) findViewById(R.id.etThings);
    hatefulAction = (EditText) findViewById(R.id.etAction);
    outro = (EditText) findViewById(R.id.etOutro);
    sendEmail = (Button) findViewById(R.id.bSentEmail);
    color = (TextView) findViewById(R.id.colors);
}

public void onClick(View v) {
    // TODO Auto-generated method stub
    color.setTextColor(Color.RED);
    convertEditTextVarsIntoStringsAndYesThisIsAMethodWeCreated();
    String emailaddress[] = { emailAdd };
    String message = "Well hello "
            + name
            + " I just wanted to say "
            + beginning
            + ".  Not only that but I hate when you "
            + stupidAction
            + ", that just really makes me crazy.  I just want to make you "
            + hatefulAct
            + ".  Welp, thats all I wanted to chit-chatter about, oh and"
            + out
            + ".  Oh also if you get bored you should check out www.mybringback.com"
            + '\n' + "PS. I think I love you...   :(";
    final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
    //Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
    emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, emailaddress);
    emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "I hate you");
    emailIntent.setType("plain/type");
    emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, message);
}

private void convertEditTextVarsIntoStringsAndYesThisIsAMethodWeCreated() {
    // TODO Auto-generated method stub
    emailAdd = personsEmail.getText().toString();
    beginning = intro.getText().toString();
    name = personsName.getText().toString();
    stupidAction = stupidThings.getText().toString();
    hatefulAct = hatefulAction.getText().toString();
    out = outro.getText().toString();
}

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    finish();
}

}

2 个答案:

答案 0 :(得分:3)

您已创建了intent,但从未表示要执行。添加以下代码:

 startActivity(Intent.createChooser(emailIntent, "Send Email"));

答案 1 :(得分:0)

尝试一下,也许你可以找到代码中缺少的东西

Intent it = new Intent(Intent.ACTION_SEND); it.setData(Uri.parse("mailto:")); String [] to = {"Your-Email@example.com"}; it.putExtra(Intent.EXTRA_EMAIL, to); it.putExtra(Intent.EXTRA_SUBJECT, "Hii this was sent from my App"); it.putExtra(Intent.EXTRA_TEXT, "Hey Whatsupp!! ,how yuo doing ???"); it.setType("message/rfc822"); chooser = Intent.createChooser(it, "Launch Email"); startActivity(chooser);