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