简体   繁体   中英

sending email and SQlite database operation one intent new thread

One of the functions in my app is sending email. The email list is generated by querying from SQLite database table. So sending email and query data from SQLite database at the same activity. It is not working. Sending email code works if I apply the code in a simple app. Query works. It is not working when I put them all together. After reading online, my feeling is that I need to create a new thread that handle the SQLite database query. I am very new for android and java and don't know how to create a new thread (background). Could somebody help me? Many many thanks!!!!!

my activity code as following:

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();
            }



        }

}

You should never execute database queries or network calls on the main thread. If you want to query a database to display data you probably want to you a AsyncTask for that.

Something like the following should work:

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("");
        }
    }       
}

Please consider renaming your method taking the java naming conventions under consideration

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM