I am using intent for send email with attachment,it is work fine,i want to get this email intent result,i already used startActivityforResult()
,but i can't get result for email intent,how can we use startActivityforResult()
for Email intent?
我正在用意向发送邮件和附件,很好,我想要得到这个邮件意向结果,我已经用了startActivityforResult(),但是我无法得到邮件意向的结果,我们怎么可以用startActivityforResult()作为邮件意向?
Thanks All
感谢所有
3 个解决方案
#1
12
You can't, this is not part of the API. It returns once you have pressed send button even if it is not sent
你不能,这不是API的一部分。一旦你按下发送按钮,它就会返回,即使它没有被发送
#2
0
There is no API like was suggested earlier. But... there is a workaround, though. The best way would be to use startActivityForResult()
, instead of startActivity()
to start the email intent. Then your onActivityResult
method should look like this:
没有像之前建议的那样的API。但是…不过,还有一个变通办法。最好的方法是使用startActivityForResult()而不是startActivity()来启动邮件意图。那么您的onActivityResult方法应该如下所示:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == YOUR_REQUEST_CODE){
if (resultCode == RESULT_OK && data != null){
//if data is not null, the user has clicked the send button
//in his email application
}
}
}
}
Remember that if you call startActivityForResult()
from the fragment, always use your activity context, like context.startActivityForResult()
and the onActivityResult
method should be overridden in your host activity.
请记住,如果您从片段中调用startActivityForResult(),请始终使用您的活动上下文,如context.startActivityForResult()和onActivityResult方法应在您的宿主活动中被覆盖。
#3
-2
You sort of can, but it's ugly and inelegant. I'll work on smoothing this out. The main problem: After the email is sent you end up at a black screen with nothing but your app title at the top.
你可以,但它很丑而且不优雅。我要把它弄平。最主要的问题是:邮件发送后,你会在一个黑色的屏幕上,除了你的应用程序标题。
I'll do a 'hit enter to continue' or something if I have to.
如果有必要的话,我会按回车键继续。
Anyway: First snippet from main class writes the report to sdcard, then calls the activity that will send email.
总之:首先来自main类的代码片段将报告写入sdcard,然后调用发送电子邮件的活动。
WriteReportToStorage();
Intent Emailreport = new Intent(bvsactivity.this, Emailreport.class);
startActivityForResult(Emailreport,emailreport_ran);
Next, over in the emailreport class we do the standard email+attachment sending code:
接下来,在emailreport类中,我们使用标准的email+附件发送代码:
public class Emailreport extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final Intent email = new Intent(android.content.Intent.ACTION_SEND);
email.setType("text/html
");
email.putExtra(android.content.Intent.EXTRA_SUBJECT, "subject");
email.putExtra(android.content.Intent.EXTRA_TEXT, "body");
email.putExtra(android.content.Intent.EXTRA_STREAM, Uri.parse("file:/" + Environment.getExternalStorageDirectory() + "//Report.html"));
startActivity(Intent.createChooser(email, "Email:"));
}
Lastly, back in your 'main' class, the onactivityresult that deletes the sdcard file:
最后,回到“main”类,删除sdcard文件的onactivityresult:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Actions based on which menu item we chose.
if (requestCode == emailreport_ran) {boolean deleted = reportfile.delete(); emailreport_ran = 1;}
}
}
#1
12
You can't, this is not part of the API. It returns once you have pressed send button even if it is not sent
你不能,这不是API的一部分。一旦你按下发送按钮,它就会返回,即使它没有被发送
#2
0
There is no API like was suggested earlier. But... there is a workaround, though. The best way would be to use startActivityForResult()
, instead of startActivity()
to start the email intent. Then your onActivityResult
method should look like this:
没有像之前建议的那样的API。但是…不过,还有一个变通办法。最好的方法是使用startActivityForResult()而不是startActivity()来启动邮件意图。那么您的onActivityResult方法应该如下所示:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == YOUR_REQUEST_CODE){
if (resultCode == RESULT_OK && data != null){
//if data is not null, the user has clicked the send button
//in his email application
}
}
}
}
Remember that if you call startActivityForResult()
from the fragment, always use your activity context, like context.startActivityForResult()
and the onActivityResult
method should be overridden in your host activity.
请记住,如果您从片段中调用startActivityForResult(),请始终使用您的活动上下文,如context.startActivityForResult()和onActivityResult方法应在您的宿主活动中被覆盖。
#3
-2
You sort of can, but it's ugly and inelegant. I'll work on smoothing this out. The main problem: After the email is sent you end up at a black screen with nothing but your app title at the top.
你可以,但它很丑而且不优雅。我要把它弄平。最主要的问题是:邮件发送后,你会在一个黑色的屏幕上,除了你的应用程序标题。
I'll do a 'hit enter to continue' or something if I have to.
如果有必要的话,我会按回车键继续。
Anyway: First snippet from main class writes the report to sdcard, then calls the activity that will send email.
总之:首先来自main类的代码片段将报告写入sdcard,然后调用发送电子邮件的活动。
WriteReportToStorage();
Intent Emailreport = new Intent(bvsactivity.this, Emailreport.class);
startActivityForResult(Emailreport,emailreport_ran);
Next, over in the emailreport class we do the standard email+attachment sending code:
接下来,在emailreport类中,我们使用标准的email+附件发送代码:
public class Emailreport extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final Intent email = new Intent(android.content.Intent.ACTION_SEND);
email.setType("text/html
");
email.putExtra(android.content.Intent.EXTRA_SUBJECT, "subject");
email.putExtra(android.content.Intent.EXTRA_TEXT, "body");
email.putExtra(android.content.Intent.EXTRA_STREAM, Uri.parse("file:/" + Environment.getExternalStorageDirectory() + "//Report.html"));
startActivity(Intent.createChooser(email, "Email:"));
}
Lastly, back in your 'main' class, the onactivityresult that deletes the sdcard file:
最后,回到“main”类,删除sdcard文件的onactivityresult:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Actions based on which menu item we chose.
if (requestCode == emailreport_ran) {boolean deleted = reportfile.delete(); emailreport_ran = 1;}
}
}