Android Intent从单独的类打开电子邮件应用程序?

时间:2012-07-27 18:25:42

标签: android android-intent

我有一个TabHost应用程序,用户可以从任何标签活动发送电子邮件。我想编写一个类,它可以从任何将处理启动Email意图的活动中实例化,但不确定这是否是理想的方法。

虽然它节省了一些代码重复,但似乎需要创建一个intent来创建另一个启动createChooser()的意图。还有更好的方法吗?

申请代码

Intent send = new Intent (this, Email.class); 
send.putExtra ("mailto", EMAIL_ADDRESS); 
send.putExtra ("subject", SUBJECT); 
send.putExtra ("body", MSG_BODY); 
this.startActivity (send);

电子邮件课程

public class Email extends Activity
{
    @Override
    protected void onCreate (Bundle savedInstanceState)
    {
        super.onCreate (savedInstanceState);
        Log.d ("email" , " oncreate");

        Bundle ex = getIntent ().getExtras ();
        String mailto =  ex.getString ("mailto");
        String subject = ex.getString ("subject");
        String body = ex.getString ("body");

        if (body == null)
           body = "";
        if (subject == null)
           subject = "";

        try
        { 
            // use the builtin chooser for users mail app or SMS
            /* NOTE: AndroidManifest has android:noHistory="true" for this */
            Intent sendIntent = new Intent(android.content.Intent.ACTION_SEND);
            sendIntent.setType("text/plain");
            sendIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String [] {mailto});
            sendIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
            sendIntent.putExtra(android.content.Intent.EXTRA_TEXT, body);
            startActivityForResult (Intent.createChooser(sendIntent, "Send via which Application?"), 0);
        }
        catch (Exception e) 
        {
            Toast.makeText (this, "No activity was found to handle this action",Toast.LENGTH_SHORT).show();
        }
    }
}

2 个答案:

答案 0 :(得分:1)

  

我想编写一个类,可以从处理启动Email意图的任何活动中实例化,但不确定这是理想的方法。

由于您的代码不起作用,它肯定不是理想的方法。

  

有更好的方法吗?

使用静态方法。将您的主题等作为参数传递给方法。让方法在startActivity() ACTION_SEND上调用Intent(注意:不要打扰调用startActivityForResult(),因为ACTION_SEND不适用于startActivityForResult() })。

答案 1 :(得分:0)

好的,事实证明,这是相当微不足道的。粘性部分正在传递Context并从中启动intent。希望它可以帮助别人。

package foo.test.email;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

    public class Email
    {

        public static void send (Context ctx, String addy, String subject, String body)
        {
            try
            {
                // use the builtin chooser for users 'send' app
                Intent sendIntent = new Intent(Intent.ACTION_SEND);
                sendIntent.setType("text/plain");

                sendIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String [] {addy});
                sendIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
                sendIntent.putExtra(android.content.Intent.EXTRA_TEXT, body);

                ctx.startActivity (Intent.createChooser(sendIntent, "Send via which Application?"));
            }
            catch (Exception e) 
            {
                Toast.makeText (ctx, "No activity was found to handle this action",Toast.LENGTH_SHORT).show();
            }
        }

    }

并从另一个类调用静态发送方法:

Email.send (mainApp.this, "who@where", "a subject", "a body");