I am working on an app with email sending function. The email recipients address list is from an SQLite query from a database. Apparently I have to run database query in the background in order to get the email function work (see my code below). However, I don't know how to get the output of my query that contains an email list and load on to a 'TO' recipients field. The Email 'TO' field should be a string array list. The current code is not working. Could someone help me on this? Maybe there are other ways to solve my problem. I am very new to Android and Java. Many thanks!
my activity codes are here:
package jhapps.com.demographics;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.io.Serializable;
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(this);
String[] emailGroupTop10=new String[dataBaseHelper.eMailListMonthTop10().size()];
for(int i=0;i<dataBaseHelper.eMailListMonthTop10().size();i++){
emailGroupTop10[i]=dataBaseHelper.eMailListMonthTop10().get(i);
}
*/
new SendEmailTop10Task().execute();
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, new SendEmailTop10Task().execute());
// 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();
}
}
class SendEmailTop10Task extends AsyncTask<Void, Void, Void> {
// This is called on a seperate thread
@Override
protected Void doInBackground(Void... Voids) {
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);
}
return null;
}
}
}
答案 0 :(得分:0)
这是我能想到的:
public String getEmaillist() {
btnSubmit = (Button) findViewById(R.id.btneMailist);
btnSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
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);
}
}
});
return null;
}
然后添加 getEmaillist(); 到onCreate
像这样修改原始代码:
String emaillist= new String();
emaillist=getEmaillist();
//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, emaillist);
这是行不通的。请提供更多信息。非常感谢