使用 android 中的帐户管理器获取所有者用户名和电话号码
这是我的代码..
public void OwnerInfo() {
final AccountManager manager = AccountManager.get(this);
final Account[] accounts = manager.getAccountsByType("com.google");
final int size = accounts.length;
String[] names = new String[size];
for (int i = 0; i < size; i++) {
names[i] = accounts[i].name;
}
if (names[0] != null) {
accountName = names[0];
ContentResolver cr = this.getContentResolver();
Cursor emailCur = cr.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Email.DATA + " = ?",
new String[] { accountName }, null);
while (emailCur.moveToNext()) {
id = emailCur
.getString(emailCur
.getColumnIndex(ContactsContract.CommonDataKinds.Email.CONTACT_ID));
email = emailCur
.getString(emailCur
.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
String newName = emailCur
.getString(emailCur
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if (name == null || newName.length() > name.length())
name = newName;
Log.v("Got contacts", "ID " + id + " Email : " + email
+ " Name : " + name);
}
emailCur.close();
if (id != null) {
// get the phone number
Cursor pCur = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = ?", new String[] { id }, null);
while (pCur.moveToNext()) {
phone = pCur
.getString(pCur
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Log.v("Got contacts", "phone" + phone);
}
pCur.close();
}
}
}
但它显示错误..
08-22 11:47:36.392: E/AndroidRuntime(434): FATAL EXCEPTION: main
08-22 11:47:36.392: E/AndroidRuntime(434): java.lang.ArrayIndexOutOfBoundsException
08-22 11:47:36.392: E/AndroidRuntime(434): at com.param.calltracker.SettingsActivity.OwnerInfo(SettingsActivity.java:142)
08-22 11:47:36.392: E/AndroidRuntime(434): at com.param.calltracker.SettingsActivity$1.onClick(SettingsActivity.java:78)
08-22 11:47:36.392: E/AndroidRuntime(434): at android.view.View.performClick(View.java:2485)
08-22 11:47:36.392: E/AndroidRuntime(434): at android.view.View$PerformClick.run(View.java:9080)
08-22 11:47:36.392: E/AndroidRuntime(434): at android.os.Handler.handleCallback(Handler.java:587)
08-22 11:47:36.392: E/AndroidRuntime(434): at android.os.Handler.dispatchMessage(Handler.java:92)
08-22 11:47:36.392: E/AndroidRuntime(434): at android.os.Looper.loop(Looper.java:123)
08-22 11:47:36.392: E/AndroidRuntime(434): at android.app.ActivityThread.main(ActivityThread.java:3683)
08-22 11:47:36.392: E/AndroidRuntime(434): at java.lang.reflect.Method.invokeNative(Native Method)
08-22 11:47:36.392: E/AndroidRuntime(434): at java.lang.reflect.Method.invoke(Method.java:507)
08-22 11:47:36.392: E/AndroidRuntime(434): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-22 11:47:36.392: E/AndroidRuntime(434): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-22 11:47:36.392: E/AndroidRuntime(434): at dalvik.system.NativeStart.main(Native Method)
我的代码中的错误在哪里..请帮助我..