按钮开放活动特定

时间:2016-03-14 05:45:24

标签: java android android-activity

我将尽可能具体。我有一个按钮。此按钮称为“发送”按钮。当用户点击按钮发送时,我希望我的应用自动将电子邮件发送到我的收件箱。问题是,每次我问这个问题,这里的人都告诉我,选择一个电子邮件客户端'。我不想要这个。这是一个场景,用户键入' HIS'电邮到第一个方框。然后把' TEXT'在下面的框中。当此用户点击“发送”时该应用程序应自动将该信息发送到我的电子邮件收件箱。

我的EmailActivity.java位于以下..

    import android.app.Activity;
    import android.content.ActivityNotFoundException;
    import android.content.ComponentName;
    import android.content.Intent;
    import android.net.Uri;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.Toast;


    public class EmailActivity extends Activity {
    ;

    Button buttonSend;
    EditText textTo;
    EditText textSubject;
    EditText textMessage;

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



    buttonSend = (Button) findViewById(R.id.buttonSend);
    textTo = (EditText) findViewById(R.id.editTextTo);
    textSubject = (EditText) findViewById(R.id.editTextSubject);
    textMessage = (EditText) findViewById(R.id.editTextMessage);

    buttonSend.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
                    "mailto","r2kbeastgaming4us.com", null));

            String to = textTo.getText().toString();
            String subject = textSubject.getText().toString();
            String message = textMessage.getText().toString();

            Intent email = new Intent(Intent.ACTION_SEND);
            email.putExtra(Intent.EXTRA_EMAIL, new String[]{ to});
            //email.putExtra(Intent.EXTRA_CC, new String[]{ to});
            //email.putExtra(Intent.EXTRA_BCC, new String[]{to});
            email.putExtra(Intent.EXTRA_SUBJECT, subject);
            email.putExtra(Intent.EXTRA_TEXT, message);

            //need this to prompts email client only
            email.setType("message/rfc822");

            startActivity(Intent.createChooser(email, "Choose an Email client :"));

        }
    });
}

}

我的email_layout在下面..

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
    android:id="@+id/textViewPhoneNo"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Email : "
    android:textAppearance="?android:attr/textAppearanceLarge" />

    <EditText
    android:id="@+id/editTextTo"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:inputType="textEmailAddress" >

    <requestFocus />

    </EditText>

    <TextView
    android:id="@+id/textViewSubject"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Subject : "
    android:textAppearance="?android:attr/textAppearanceLarge" />

    <EditText
    android:id="@+id/editTextSubject"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    >
    </EditText>

    <TextView
    android:id="@+id/textViewMessage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Message : "
    android:textAppearance="?android:attr/textAppearanceLarge" />

    <EditText
    android:id="@+id/editTextMessage"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="top"
    android:inputType="textMultiLine"
    android:lines="5" />

    <Button
    android:id="@+id/buttonSend"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Send" />

    </LinearLayout>

1 个答案:

答案 0 :(得分:1)

你必须从你的应用程序中明确调用gmail,为此你必须提到包名。试试这段代码

Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
                    "mailto","r2kbeastgaming4us.com", null));

            String to = textTo.getText().toString();
            String subject = textSubject.getText().toString();
            String message = textMessage.getText().toString();

            Intent email = new Intent(Intent.ACTION_SEND);
            email.putExtra(Intent.EXTRA_EMAIL, new String[]{ to});
            //email.putExtra(Intent.EXTRA_CC, new String[]{ to});
            //email.putExtra(Intent.EXTRA_BCC, new String[]{to});
            email.putExtra(Intent.EXTRA_SUBJECT, subject);
            email.putExtra(Intent.EXTRA_TEXT, message);

            //need this to prompts email client only
            email.setType("message/rfc822");

          **email.setPackage("com.google.android.gm");**

            startActivity(Intent.createChooser(email, "Choose an Email client :"));