我正在构建适用于Android 4.0(Ice Cream Sandwich)及更高版本的应用,并希望获得设备所有者的用户个人资料信息,例如:
DisplayName,电子邮件,电话,职位,公司,办公地点,网站和网址
我尝试过不同的Stackoverflow帖子中的不同方法,包括访问手机的帐户查询用户个人资料(ContactsContract.Profile.ContentUri)。没有骰子。我已经检查了ReadOwnerData和ReadProfile的权限。
现在我正在使用此代码查询“联系人”,但托管查询的返回值为0.
var c = act.ManagedQuery (ContactsContract.Profile.ContentUri, null, null, null, null);
int count = c.Count;
string[] columnNames = c.GetColumnNames();
bool b = c.MoveToFirst();
int position = c.Position;
if (position == 0) {
do {
for (int j = 0; j < columnNames.Length; j++) {
string columnName = columnNames[j];
try {
string columnValue = c.GetString(c.GetColumnIndex(columnName));
} catch {}
if (c.GetString(c.GetColumnIndex("is_user_profile")) == "1") {
if (c.GetString(c.GetColumnIndex("has_email")) == "1") {
//var cEmail = ManagedQuery (ContactsContract.Data.ContentUri, null, ContactsContract.Data.InterfaceConsts.HasPhoneNumber + "!=0 AND (" + ContactsContract.Data.InterfaceConsts.Mimetype + "=? OR " + ContactsContract.Data.InterfaceConsts.Mimetype + "=?)", new String[]{ ContactsContract.CommonDataKinds.Email.ContentItemType, ContactsContract.CommonDataKinds.Phone.ContentItemType, ContactsContract.Contacts.InterfaceConsts.Id }, null);
var cEmail = act.ManagedQuery (ContactsContract.CommonDataKinds.Email.ContentUri, null, ContactsContract.CommonDataKinds.Email.InterfaceConsts.Type + " = ?", new String[]{ c.GetString(c.GetColumnIndex(BaseColumns.Id)) }, null);
string[] columnNamesEmail = cEmail.GetColumnNames();
if (cEmail.Count > 0) {
cEmail.MoveToFirst();
do {
sEmail = cEmail.GetString(cEmail.GetColumnIndex(ContactsContract.CommonDataKinds.Email.InterfaceConsts.Type));
//sEmail = cEmail.GetString(cEmail.GetColumnIndex(ContactsContract.CommonDataKinds.Email.InterfaceConsts.Data));
//sEmail = cEmail.GetString(cEmail.GetColumnIndex(ContactsContract.CommonDataKinds.Email.InterfaceConsts.ContactId));
} while (cEmail.MoveToNext());
}
cEmail.Close();
}
if (c.GetString(c.GetColumnIndex("has_phone_number")) == "1") {
var cPhone = act.ManagedQuery (ContactsContract.CommonDataKinds.Phone.ContentUri, null, ContactsContract.CommonDataKinds.Phone.InterfaceConsts.ContactId + " = ?", new String[]{ c.GetString(c.GetColumnIndex(BaseColumns.Id)) }, null);
string[] columnNamesPhone = cPhone.GetColumnNames();
if (cPhone.Count > 0) {
cPhone.MoveToFirst();
do {
sPhone = cPhone.GetString(cPhone.GetColumnIndex(ContactsContract.CommonDataKinds.Phone.InterfaceConsts.Type));
} while (cPhone.MoveToNext());
}
cPhone.Close();
}
}
}
} while (c.MoveToNext());
}
c.Close();
这部分代码返回0:
if (cEmail.Count > 0) {
有人可以指出我如何重写这个查询吗?
var cEmail = act.ManagedQuery (ContactsContract.CommonDataKinds.Email.ContentUri, null, ContactsContract.CommonDataKinds.Email.InterfaceConsts.Type + " = ?", new String[]{ c.GetString(c.GetColumnIndex(BaseColumns.Id)) }, null);
或者我应该回去查询用户个人资料或帐户?这是我为他们使用的代码:
账户:
Account[] accounts = AccountManager.Get (Context).GetAccounts ();
foreach (Account acc in accounts) {
--- can only get DisplayName and ID
}
用户个人资料:
var uri = ContactsContract.Profile.ContentUri;
string[] projection = {
ContactsContract.CommonDataKinds.StructuredName.DisplayName,
ContactsContract.CommonDataKinds.Email.Address,
ContactsContract.CommonDataKinds.Phone.Number,
ContactsContract.CommonDataKinds.Organization.Title,
ContactsContract.CommonDataKinds.Organization.Company,
ContactsContract.CommonDataKinds.Organization.OfficeLocation,
ContactsContract.CommonDataKinds.Website.Url };
var cursor = ManagedQuery (uri, projection, null, null, null);
if (cursor.MoveToFirst ()) {
do {
sDisplayName = cursor.GetString (cursor.GetColumnIndex (projection [0]));
sEmail = cursor.GetString (cursor.GetColumnIndex (projection [1]));
sPhone = cursor.GetString (cursor.GetColumnIndex (projection [2]));
sTitle = cursor.GetString (cursor.GetColumnIndex (projection [3]));
sCompany = cursor.GetString (cursor.GetColumnIndex (projection [4]));
sOfficeLocation = cursor.GetString (cursor.GetColumnIndex (projection [5]));
sWebsite = cursor.GetString (cursor.GetColumnIndex (projection [6]));
} while (cursor.MoveToNext());
}