我正在尝试通过我的应用访问联系人。所以我能够通过邮件分享信息给朋友。到目前为止,联系人已显示,但当我点击要添加到电子邮件的名称时,应用程序只返回上一个屏幕,并且不会发送电子邮件。编辑文本未设置为我选择的联系人。
Android清单
<uses-permission android:name="android.permission.READ_CONTACTS"> </uses-permission>
EmailSend课程
contacts.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,
Contacts.CONTENT_URI);
startActivityForResult(contactPickerIntent, CONTACT_PICKER_RESULT);
}
});
...
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
System.out.println("yo");
switch (requestCode) {
case CONTACT_PICKER_RESULT:
Cursor cursor = null;
String email = "";
System.out.println("Hello");
try {
Uri result = data.getData();
Log.v("DEBUG: ", "Got a contact result: "
+ result.toString());
// get the contact id from the Uri
String id = result.getLastPathSegment();
// query for everything email
cursor = getContentResolver().query(Email.CONTENT_URI,
null, Email.CONTACT_ID + "=?", new String[] { id },
null);
int emailIdx = cursor.getColumnIndex(Email.DATA);
// let's just get the first email
if (cursor.moveToFirst()) {
email = cursor.getString(emailIdx);
Log.v("DEBUG: ", "Got email: " + email);
} else {
Log.w("DEBUG: ", "No results");
}
} catch (Exception e) {
Log.e("DEBUG: ", "Failed to get email data", e);
} finally {
if (cursor != null) {
cursor.close();
}
// EditText emailEntry = (EditText) findViewById(R.id.invite_email);
personsEmail.setText(email);
if (email.length() == 0) {
Toast.makeText(this, "No email found for contact.",
Toast.LENGTH_LONG).show();
}
}
break;
}
} else {
Log.w("DEBUG: ", "Warning: activity result not ok");
}
}
更令人费解的是,logcat上没有任何东西出现!所以它超过了我的所有错误检查。知道为什么屏幕崩溃和电子邮件地址没有添加到EditText?
答案 0 :(得分:1)
如果您在最新的操作系统中测试它,这意味着Android M,他们引入了一组新的权限,称为运行时权限。
http://developer.android.com/training/permissions/requesting.html
请检查此链接,如果这样可以解决问题,请告诉我们。
答案 1 :(得分:0)
Try this
public void getNumber(ContentResolver cr) {
Cursor phones = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,
null, null);
while (phones.moveToNext()) {
ContactSynRequestcontacts data = new ContactSynRequestcontacts();
String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
游标电子邮件= contResv.query(Email.CONTENT_URI,null,Email.CONTACT_ID +“=”+ contactId,null,null); while(emails.moveToNext()) { String email = emails.getString(emails.getColumnIndex(Email.DATA)); 打破; } emails.close();
data.setName(name);
data.setPhoneNumber(phoneNumber);
userContactList.add(data);
}
phones.close();// close cursor
}
public class ContactSynRequestcontacts {
private String Name;
public String getName() {
return Name;
}
public void setName(String name) {
this.Name = name;
}
private String PhoneNumber;
public String getPhoneNumber() {
return PhoneNumber;
}
public void setPhoneNumber(String mobile) {
this.PhoneNumber = mobile;
}
}