0

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

尽管它节省了一些代码重复,但似乎必须创建一个意图来创建另一个意图来启动 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();
        }
    }
}
4

2 回答 2

1

我想编写一个可以从任何将处理启动电子邮件意图的活动实例化的类,但不确定这是实现它的理想方式。

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

有没有更好的办法?

使用静态方法。将您的主题等作为参数传递给该方法。让方法调用startActivity()你的ACTION_SEND Intent(注意:不要打扰调用startActivityForResult(),因为ACTION_SEND它不是为使用而设计的startActivityForResult())。

于 2012-07-27T18:46:22.637 回答
0

好的,事实证明,这是相当微不足道的。棘手的部分是传递上下文并从中启动意图。希望它可以帮助别人。

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");
于 2012-07-30T18:28:05.397 回答