Android 中的 AsyncTask 参数

发布于 2024-12-25 14:09:16 字数 680 浏览 1 评论 0原文

我正在创建一个应用程序,用户只需单击按钮即可发送电子邮件。 但我希望发送电子邮件的任务应该在后台完成。 有人建议我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北方。的韩爷 2025-01-01 14:09:16

在 onClick 调用中

new SendEmail(0).execute();

/** Inner class for implementing progress bar before fetching data **/
    private class SendEmail extends AsyncTask<Void, Void, Integer> 
    {
//if you want to show progress you can show here

  //  private ProgressDialog Dialog = new ProgressDialog(yourActivityClass.this);
    @Override
    protected void onPreExecute()
    {
//           Dialog.setMessage("Doing something...");
   //     Dialog.show();
    }

    @Override
    protected Integer doInBackground(Void... params) 
    {
        //Task for sending mail

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);   
    } 
        return 0;
    }

    @Override
    protected void onPostExecute(Integer result)
        {

        if(result==0)
        {
//do some thing
        }

// after completed finished the progressbar
 //       Dialog.dismiss();
    }

in onClick call

new SendEmail(0).execute();

/** Inner class for implementing progress bar before fetching data **/
    private class SendEmail extends AsyncTask<Void, Void, Integer> 
    {
//if you want to show progress you can show here

  //  private ProgressDialog Dialog = new ProgressDialog(yourActivityClass.this);
    @Override
    protected void onPreExecute()
    {
//           Dialog.setMessage("Doing something...");
   //     Dialog.show();
    }

    @Override
    protected Integer doInBackground(Void... params) 
    {
        //Task for sending mail

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);   
    } 
        return 0;
    }

    @Override
    protected void onPostExecute(Integer result)
        {

        if(result==0)
        {
//do some thing
        }

// after completed finished the progressbar
 //       Dialog.dismiss();
    }
眼睛会笑 2025-01-01 14:09:16
private class YourTask extends AsyncTask<String, Void, Void> {

        @Override
        protected Void doInBackground(String... params) {
            String email = params[0];
                    String name = params[1];


                        // do whatever with your strings..
        }

        @Override
        protected void onPostExecute() {

        }

    }

new YourTask().execute("[email protected]", "Test User name");

AsyncTask 的第一个参数是您的输入参数类型(您需要的是 String),第二个参数在执行进程时使用,例如您可以更新进度指示器,最后第三个参数是您的返回参数。如果您想根据输出显示警报或其他内容,可以通过此参数将参数传递给 onPostExecute 方法。

如果您想显示进度对话框,您应该重写onPreExecute函数。

希望这有帮助。

private class YourTask extends AsyncTask<String, Void, Void> {

        @Override
        protected Void doInBackground(String... params) {
            String email = params[0];
                    String name = params[1];


                        // do whatever with your strings..
        }

        @Override
        protected void onPostExecute() {

        }

    }

new YourTask().execute("[email protected]", "Test User name");

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文