【问题标题】:Android Intent to open Email application from separate class?Android打算从单独的类打开电子邮件应用程序?
【发布时间】:2012-07-26 11:19:54
【问题描述】:

我有一个 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();
        }
    }
}

【问题讨论】:

    标签: android android-intent


    【解决方案1】:

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

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

    有没有更好的办法?

    使用静态方法。将您的主题等作为参数传递给该方法。在您的ACTION_SEND Intent 上调用startActivity() 方法(注意:不要打扰调用startActivityForResult(),因为ACTION_SEND 并非设计用于与startActivityForResult() 一起使用)。

    【讨论】:

    • 哦,它可能不适合你,因为为了简洁起见,我省略了导入部分。您需要先添加这些。我尝试为“send()”创建一个静态方法,但由于 Activity 本身不是静态的,我无法在静态方法中调用 startActivity()。不过谢谢,我会再多看看。
    • @wufoo:“为简洁起见,我省略了导入部分”——不,你有比这更根本的问题。如果您尝试通过 Intent extras 将数据传递给自己,那么从 extras 中读取该数据通常很有用,而您并没有这样做。 “我无法在静态方法中调用 startActivity()”——当然可以。将合适的 Context(例如,现有的 Activity 实例)作为参数传递给您的静态方法。
    • facepalm 我明白你现在的意思了。是的,我遗漏了我在发布过程中所做的编辑。我会更新代码。
    【解决方案2】:

    好吧,事实证明,这是相当微不足道的。棘手的部分是传递上下文并从中启动意图。希望对其他人有所帮助。

    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");
    

    【讨论】:

    • startActivity()Context 上的一个方法,所以你不需要强制转换它。此外mainApp.thismainApp 相同,因此您可以删除.this 部分。
    • 好的,谢谢。我已经更新了代码。我从 MainApp 的 OnItemClickListener 调用 Email.send()。如果我没有专门使用“MainApp.this”,Eclipse 认为“this”指的是(据我所知)OnItemClickListener 而不是 MainApp 上下文。我想也可以使用 getBaseContext() 来完成?
    • 哦,对不起,我错过了。然后你会想要MainApp.this
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-30
    • 2013-08-30
    • 2011-01-03
    • 2017-06-11
    • 2018-01-31
    相关资源
    最近更新 更多