Android 系列 4.2从视图发送电子邮件文本

来源:互联网 发布:电话轰炸机淘宝叫什么 编辑:程序博客网 时间:2024/05/29 19:46
4.2从视图发送电子邮件文本


问题
您想要从视图发送包含文本或图像的电子邮件。

将要通过电子邮件发送到邮件应用程序的数据作为参数传递给intent。
讨论
从视图发送电子邮件文本的步骤非常简单:
1.修改AndroidManifest.xml文件以允许Internet连接,以便可以发送电子邮件。 这在实施例4-1中示出。
2.使用用户单击的电子邮件按钮创建视觉表示层。 布局如例4-2所示,用于填充它的字符串如例4-3所示。
3.附加OnClickListener以允许在用户单击电子邮件按钮时发送电子邮件。 代码如例4-4所示。

实例4-1。 AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?><manifestxmlns:android="http://schemas.android.com/apk/res/android"package="com.examples"android:versionCode="1"android:versionName="1.0"><uses-permissionandroid:name="android.permission.INTERNET" /><applicationandroid:icon="@drawable/icon"android:label="@string/app_name"><activityandroid:name=".Main"android:label="@string/app_name"><intent-filter><actionandroid:name="android.intent.action.MAIN" /><categoryandroid:name="android.intent.category.LAUNCHER" /></intent-filter></activity></application></manifest>


实例4-2。 Main.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"><Buttonandroid:id="@+id/emailButton"android:text="Email Text!"android:layout_width="fill_parent"android:layout_height="wrap_content"></Button><TextViewandroid:id="@+id/text_to_email"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="@string/my_text" /></LinearLayout>


实例4-3。Strings.xml

<?xml version="1.0" encoding="utf-8"?><resources><stringname="hello">Hello World, Main!</string><stringname="app_name">EmailFromView</string><stringname="my_text">Lorem Ipsum is simply dummy text of the printing and typesetting industry. LoremIpsum has been the industry's standard dummy text ever since the 1500s, whenan unknown printer took a galley of type and scrambled it to make a typespecimen book. It has survived not only five centuries, but also the leap intoelectronic typesetting, remaining essentially unchanged. It was popularised inthe 1960s with the release of Letraset sheets containing Lorem Ipsum passages,and more recently with desktop publishing software like Aldus PageMakerincluding versions of Lorem Ipsum.</string></resources>

实例4-4。 Main.java

import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class Main extends Activity implements OnClickListener {private static final String TAG = "Main";private Button emailButton;/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);// Set the View LayersetContentView(R.layout.main);// Get reference to Email Buttonthis.emailButton = (Button) this.findViewById(R.id.emailButton);// Sets the Event Listener onClickthis.emailButton.setOnClickListener(this);}@Overridepublic void onClick(View view) {if (view == this.emailButton) {Intent emailIntent = new Intent(Intent.ACTION_SEND);emailIntent.setType("text/html");emailIntent.putExtra(Intent.EXTRA_TITLE, "My Title");emailIntent.putExtra(Intent.EXTRA_SUBJECT, "My Subject");// Obtain reference to String and pass it to IntentemailIntent.putExtra(Intent.EXTRA_TEXT,getString(R.string.my_text));startActivity(emailIntent);}}}


0 0
原创粉丝点击