简体   繁体   中英

Android: Opening an email application on clicking the label

<TextView
    android:id="@+id/TextView03"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/header"
    android:layout_alignLeft="@+id/button1"
    android:layout_marginBottom="127dp"
    android:text="@string/email"
    android:textColor="#000000"
    android:textSize="12dp"
    android:typeface="sans" />

I have a test view which holds my email information. How do i open an default email client on the click of the message.

<string name="email">E-mail:email@gmail.com</string>

Here is my Override method.

    @Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    TextView t2 = (TextView) findViewById(R.id.TextView03);
            email.setOnClickListener(new View.OnClickListener() {

                 public void onClick(View v) {
                     // TODO Auto-generated method stub                
                 }

           });
}

What should i do from here?

Intent i= new Intent(android.content.Intent.ACTION_SEND);
i.setType("plain/text");
i.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{"email@email.com"});
i.putExtra(android.content.Intent.EXTRA_SUBJECT, mySubject);
i.putExtra(android.content.Intent.EXTRA_TEXT, myBodyText); 
context.startActivity(Intent.createChooser(i, "Send mail...));
Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.setClassName("com.google.android.gm", "com.google.android.gm.ComposeActivityGmail");
sendIntent.putExtra(Intent.EXTRA_EMAIL, new String[] { "email@gmail.com" });
sendIntent.setData(Uri.parse("email@gmail.com"));
sendIntent.putExtra(Intent.EXTRA_SUBJECT, "enter subject");
sendIntent.setType("plain/text");
sendIntent.putExtra(Intent.EXTRA_TEXT, "Insert text");
startActivity(sendIntent);

I know its very late, nevertheless just wanted to share something that I found much easier! If you are displaying email in a textview, then use:

<TextView
...
android:text="Email: username@gmail.com"
android:autoLink="email"
/>

This automatically will open the preferred email app on the users phone with the "To:" address prefilled with the email ID mentioned above.

Try This Code :

Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.setClassName("com.google.android.gm", "com.google.android.gm.ComposeActivityGmail");
sendIntent.putExtra(Intent.EXTRA_EMAIL, new String[] { "demo@gmail.com" });
sendIntent.setData(Uri.parse("demo@gmail.com"));
sendIntent.putExtra(Intent.EXTRA_SUBJECT, "subject");
sendIntent.setType("plain/text");
sendIntent.putExtra(Intent.EXTRA_TEXT, "Insert text");
startActivity(sendIntent);

Just use Linkify on your TextView,

TextView t2 = (TextView) findViewById(R.id.TextView03);
t2.setText("E-mail:email@gmail.com");
Linkify.addLinks(t2, Linkify.EMAIL_ADDRESSES);

To do so your text view will show this email address as Hyperlink on which you can click and choose the appropriate provider to send email on given email address.

不要忘记将文本设置为android:clickable:“ true”

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM