Android 中的 AsyncTask 参数
我正在创建一个应用程序,用户只需单击按钮即可发送电子邮件。 但我希望发送电子邮件的任务应该在后台完成。 有人建议我使用 AsyncTask 但我完全不知道这一点。 有人能告诉我该怎么做吗? 我有 sendEmail 函数,它接受两个字符串参数(用户名、密码),但我不知道如何将这两个字符串参数传递给 AsyncTask 和 doInBackground 方法。 我的 sendEmail 功能是
public void sendEmail(String ownerEmailid, String datapwd )
{
try {
GMailSender sender = new GMailSender( ownerEmailid, datapwd );
sender.sendMail( "This is Subject",
"Hey buddy listen to this station it is awesome",
ownerEmailid,
email );
} catch( Exception e ) {
Log.e("SendMail", e.getMessage(), e);
}
}
请建议解决方案。
I am creating an app in which on clicking on button user can send email.
But I want the task of sending email should be done in Background.
Someone suggested me to use AsyncTask but I am totally unaware of this.
can anybody tell me how to do it ?
I have sendEmail function which takes two string parameters(username , password) and I don't know how to pass that two string args to AsyncTask and to doInBackground method.
my sendEmail function is
public void sendEmail(String ownerEmailid, String datapwd )
{
try {
GMailSender sender = new GMailSender( ownerEmailid, datapwd );
sender.sendMail( "This is Subject",
"Hey buddy listen to this station it is awesome",
ownerEmailid,
email );
} catch( Exception e ) {
Log.e("SendMail", e.getMessage(), e);
}
}
please suggest solution.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 onClick 调用中
in onClick call
AsyncTask 的第一个参数是您的输入参数类型(您需要的是 String),第二个参数在执行进程时使用,例如您可以更新进度指示器,最后第三个参数是您的返回参数。如果您想根据输出显示警报或其他内容,可以通过此参数将参数传递给 onPostExecute 方法。
如果您想显示进度对话框,您应该重写onPreExecute函数。
希望这有帮助。
First parameter of AsyncTask is your input paramater type (Your need is String), second parameter is used when process is being executed, for example you can update your progress indicator, and finally third parameter is your return parameter. If you want to show an alert or whatever according to your output, you can pass parameters to onPostExecute method by this parameters.
If you want to show an progress dialog you should override onPreExecute function.
Hope this helps.