我解決了這個問題,和代碼,這將在下文介紹,已經在我的應用程序應用「郵件圖片自動」:
https://play.google.com/store/apps/details?id=com.alexpap.EmailPicturesFree
這個程序會自動發送每一個畫面您對收件人電子郵件列表。以下功能用於選擇收件人列表,它將接收您製作的圖片。圖片即刻發送。 整個代碼位於EmailParametersActivity類中。適用於minSdkVersion =「5」。
//class variables
private EditText emailSendEdit;
private EditText passwordEdit;
private EditText passwordRepeatEdit;
private EditText emailReceiveEdit;
//array with all contacts used in multiselection dialog. Each entry is a string - name and email separated with new line (\n)
public static String[] contactArray;
public static List<Integer> mSelectedItems = new ArrayList<Integer>(); //list of selected item IDs in multiselection dialog
//list of all contacts - name and email separated with new line (\n) for each entry (contact)
public static List<String> contactList;
//一些在onCreate()方法
emailSendEdit = (EditText) findViewById(R.id.send_email);
passwordEdit = (EditText) findViewById(R.id.password);
passwordRepeatEdit = (EditText) findViewById(R.id.password_repeat);
emailReceiveEdit = (EditText) findViewById(R.id.receive_email);
emailReceiveEdit.setOnLongClickListener(new myLongListener());
的onCreate的
// end()方法
private class myLongListener implements View.OnLongClickListener {
@SuppressWarnings("deprecation")
@Override
public boolean onLongClick(View v) {
if (contactList == null || contactList.isEmpty()) {
//populate contacts sorted ahphabetically
contactList = populateContacts(EmailParametersActivity.this);
contactArray = new String[contactList.size()];
contactList.toArray(contactArray);
}
showDialog (0);
return false;
}
}
//We enter here only once, when the dialog is opened for the first time
@Override
protected Dialog onCreateDialog(int id)
{
mSelectedItems = new ArrayList();
return new AlertDialog.Builder(this)
.setTitle("Contacts")
.setMultiChoiceItems(contactArray, null, new DialogSelectionClickHandler() {
@Override
public void onClick(DialogInterface dialog, int which,
boolean isChecked) {
if (isChecked) {
// If the the item is checked, add its ID to the selected IDs
mSelectedItems.add(which);
} else if (mSelectedItems.contains(which)) {
// Else, if the ID of the item is already in the array, remove it
mSelectedItems.remove(Integer.valueOf(which));
}
}
})
.setPositiveButton("OK", new DialogButtonClickHandler())
.setNegativeButton("Cancel", new DialogButtonClickHandler())
.create();
}
//We enter here every time the dialog is opened
@Override
protected void onPrepareDialog(int id, Dialog dialog) {
//clear all previously selected item IDs
mSelectedItems.clear();
AlertDialog alert = (AlertDialog) dialog;
//get List of dialog checked items
ListView dialogListView = alert.getListView();
//uncheck all previously checked items
for(int i = 0 ; i < dialogListView.getCount(); i++){
dialogListView.setItemChecked(i, false);
}
}
//nothing to do here, we expect the result - list of checked items on click of OK button
public class DialogSelectionClickHandler implements DialogInterface.OnMultiChoiceClickListener
{
public void onClick(DialogInterface dialog, int clicked, boolean selected)
{
Log.i("ME", contactArray[ clicked ] + " selected: " + selected);
}
}
//Here we take the result - the list of checked items, from which we construct the string with selected emails
//separated with coma
public class DialogButtonClickHandler implements DialogInterface.OnClickListener
{
public void onClick(DialogInterface dialog, int clicked)
{
switch(clicked)
{
case DialogInterface.BUTTON_POSITIVE:
String receiveEmailList = getSelectedItems();
String strReceiveEmail = emailReceiveEdit.getText().toString();
if (strReceiveEmail != null && strReceiveEmail.length() > 0){
if (receiveEmailList.length() > 0){
strReceiveEmail = strReceiveEmail + "," + receiveEmailList;
}
} else {
strReceiveEmail = receiveEmailList;
}
emailReceiveEdit.setText(strReceiveEmail);
break;
case DialogInterface.BUTTON_NEGATIVE:
dialog.dismiss();
break;
}
}
}
//we build the result. It is a string of selected items. Each item is a string with format - "name \n email".
//We take only the email in the result string;
protected String getSelectedItems(){
String emailList = "";
for(int i = 0; i < mSelectedItems.size(); i++){ //number of selected items in the dialog.
int j = (int) mSelectedItems.get(i); // get the ID of current selected item. It is equal to j;
Log.i("ME", contactArray[ i ] + " selected: " + contactArray[ j ]);
String [] nameEmail = contactArray[ j ].split("\n"); //get email only
emailList = emailList + nameEmail[1] + ","; // createte a string with selected emails, separated with coma.
}
//remove the last coma in the string
if (emailList.length() > 1) {
emailList = emailList.substring(0, emailList.length() - 1);
}
//if nothing selected return emty string.
if (emailList.indexOf("@") < 1){
emailList = "";
}
return emailList;
}
最後在這裏的定義是它得到conacts姓名和功能電子郵件
@SuppressLint("InlinedApi")
public List populateContacts(Context context) {
ContentResolver cr = context.getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, "phonetic_name"); // "display_name_source");
List <String> contactList = new ArrayList <String>();
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
//Get ID and Name form CONTACTS CONTRACTS
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
//While we have cursor get the email
Cursor emailCur = cr.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null, ContactsContract.CommonDataKinds.Email.CONTACT_ID
+ " = ?", new String[] { id }, null);
while (emailCur.moveToNext()) {
// This would allow you get several email addresses
// if the email addresses were stored in an array
String email = emailCur
.getString(emailCur
.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
contactList.add(name + "\n" + email);
}
emailCur.close();
}
}
cur.close();
//Sort contact list alphabetically - ascending (without fist item)
List<String> subList = contactList.subList(1, contactList.size());
Collections.sort(subList);
return contactList;
}