使用隐式意图发送电子邮件
问题描述:
以下是用于发送电子邮件的java和xml文件。请验证代码。我已经注册了该按钮,但它仍然无法正常工作。使用隐式意图发送电子邮件
ERROR: Failed to set EGL_SWAP_BEHAVIOR on surface 0xe2d19220, error=EGL_SUCCESS.Skipped 37 frames! The application may be doing too much work on its main thread
MainActivity.java
public void onClick(View view) {
Intent intent = null,
intent = new Intent(Intent.ACTION_SEND);
intent.setData(Uri.parse("mailto:"));
String[] to = {"[email protected]", ""};
intent.putExtra(Intent.EXTRA_EMAIL, to);
intent.putExtra(Intent.EXTRA_SUBJECT, "subject to your app");
intent.putExtra(Intent.EXTRA_TEXT, "text inside email");
intent.setType("message/rfc822");
chooser = Intent.createChooser(intent, "Send email");
startActivity(chooser);
}
activity_main.xml中
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send Email"
/>
答
这里是如何从xml (btn1)
和code (btn2)
连线按钮。
MainActivity.java
package *.com.saturday;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// thru code (btn2)
Button button = (Button)findViewById(R.id.btn2);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setData(Uri.parse("mailto:"));
String[] to = {"[email protected]", ""};
intent.putExtra(Intent.EXTRA_EMAIL, to);
intent.putExtra(Intent.EXTRA_SUBJECT, "subject to your app");
intent.putExtra(Intent.EXTRA_TEXT, "text inside email");
intent.setType("message/rfc822");
Intent chooser = Intent.createChooser(intent, "Send email");
startActivity(chooser);
}
});
}
// thru xml (btn1)
@Override
public void onClick(View view) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setData(Uri.parse("mailto:"));
String[] to = {"[email protected]", ""};
intent.putExtra(Intent.EXTRA_EMAIL, to);
intent.putExtra(Intent.EXTRA_SUBJECT, "subject to your app");
intent.putExtra(Intent.EXTRA_TEXT, "text inside email");
intent.setType("message/rfc822");
Intent chooser = Intent.createChooser(intent, "Send email");
startActivity(chooser);
}
}
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="*.com.saturday.MainActivity">
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Send Email from xml"
android:onClick="onClick"/>
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/btn1"
android:layout_marginTop="10dp"
android:text="Send Email from mainactivity"/>
</RelativeLayout>
请解释 “详细” 什么 “不工作” 的意思。请注意,[ACTION_SENDTO'没有记录以支持任何这些附加内容](https://developer.android.com/reference/android/content/Intent.html#ACTION_SENDTO),并且'Uri'应该是识别收件人。 – CommonsWare
点击一个按钮,什么也没有发生。我在这里做错了什么? – Honey
也许你没有把这个'onClick()'方法连接到按钮。它应该开始一个活动,或者你应该用'ActivityNotFoundException'来崩溃。 – CommonsWare