【问题标题】:sending email and SQlite database operation one intent new thread发送电子邮件和 SQlite 数据库操作一意图新线程
【发布时间】:2016-11-12 06:46:53
【问题描述】:

我的应用程序中的一个功能是发送电子邮件。电子邮件列表是通过从 SQLite 数据库表中查询生成的。因此,在同一活动中从 SQLite 数据库发送电子邮件和查询数据。它不工作。如果我在一个简单的应用程序中应用代码,则发送电子邮件代码有效。查询有效。当我把它们放在一起时它不起作用。网上看了下,感觉是需要新建一个线程来处理SQLite数据库查询。我对 android 和 java 很陌生,不知道如何创建一个新线程(背景)。 有人可以帮我吗?非常感谢!!!!!!

我的活动代码如下:

package jhapps.com.demographics;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class PromotionEmailMonthTop10 extends Activity {
private EditText subjectGroupTop10,bodyGroupTop10;
private Button  btnMonthTop10;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_promotion_email_month_top10);


    subjectGroupTop10=(EditText)findViewById(R.id.subjectMonthTop10);
    bodyGroupTop10=(EditText)findViewById(R.id.bodyMonthTop10);
    btnMonthTop10=(Button)findViewById(R.id.btnMonthTop10);
    btnMonthTop10.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            EmailMonthTop10();

            // after sending the email, clear the fields

            subjectGroupTop10.setText("");
            bodyGroupTop10.setText("");
        }


    });


}

//get month top 10 email list
protected void EmailMonthTop10() {



            DataBaseHelper dataBaseHelper=new DataBaseHelper(PromotionEmailMonthTop10.this);
            String[] emailGroupTop10=new String[dataBaseHelper.eMailListMonthTop10().size()];
            for(int i=0;i<dataBaseHelper.eMailListMonthTop10().size();i++){
                emailGroupTop10[i]=dataBaseHelper.eMailListMonthTop10().get(i);
            }



    String subjects=subjectGroupTop10.getText().toString();
    String bodytext=bodyGroupTop10.getText().toString();

    //start email intent

            Intent email = new Intent(Intent.ACTION_SENDTO);

            // prompts email clients only
            email.setType("message/rfc822");
            email.setData(Uri.parse("mailto:"));


          email.putExtra(Intent.EXTRA_EMAIL,emailGroupTop10 );
          // email.putExtra(Intent.EXTRA_EMAIL,new String []{"junrudeng@gmail.com","huangji8@gmail.com"});

            email.putExtra(Intent.EXTRA_SUBJECT, subjects);

            email.putExtra(Intent.EXTRA_TEXT, bodytext);
            try {
                // the user can choose the email client

                startActivity(Intent.createChooser(email, "Choose an email client from..."));
            } catch (android.content.ActivityNotFoundException ex) {

                Toast.makeText(PromotionEmailMonthTop10.this, "No email client installed.",
                        Toast.LENGTH_LONG).show();
            }



        }

}

【问题讨论】:

    标签: java android multithreading sqlite email


    【解决方案1】:

    您不应该在主线程上执行数据库查询或网络调用。如果您想查询数据库以显示数据,您可能需要AsyncTask

    类似下面的东西应该可以工作:

    public class PromotionEmailMonthTop10 extends Activity {
    
        ...
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            ...
    
            btnMonthTop10.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    new SendEmailTop10Task().execute();
                }
            });
        }
    
    
        class SendEmailTop10Task extends AsyncTask<Void, Void, Void> {
    
            // This is called on a seperate thread
            @Override
            protected Void doInBackground(Void... voids) {
                EmailMonthTop10();
            }
    
            // This is called on the main thread
            @Override
            protected void onPostExecute(Integer status) {
                subjectGroupTop10.setText("");
                bodyGroupTop10.setText("");
            }
        }       
    }
    

    请考虑重命名您的方法并考虑java naming conventions

    【讨论】:

    • 非常感谢 Endzeit。这正是我的目标,但我不知道如何编码。我会按照你的建议去做,很快就会让你回来,我会得到结果。 :)
    • 我必须添加 return null 短语并摆脱 @override onPostExecute... 它仍然无法正常工作。请指教:)
    猜你喜欢
    • 1970-01-01
    • 2012-01-31
    • 1970-01-01
    • 1970-01-01
    • 2012-03-29
    • 2011-11-02
    • 1970-01-01
    • 2022-07-09
    • 1970-01-01
    相关资源
    最近更新 更多