0

使用 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)

我的代码中的错误在哪里..请帮助我..

4

2 回答 2

0

此代码将为所有者提供完整信息
尝试此代码:-

public class EmailFetcher {



        static String getName(Context context) {
            Cursor CR=null;
            CR=getOwner(context);
            String id="",name="";
            while (CR.moveToNext()) {
                name = CR
                        .getString(CR
                                .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            }

            return name;
        }
        static String getEmailId(Context context) {

            Cursor CR=null;
            CR=getOwner(context);
            String id="",email="";
            while (CR.moveToNext()) {
                id = CR.getString(CR
                        .getColumnIndex(ContactsContract.CommonDataKinds.Email.CONTACT_ID));
                email = CR
                        .getString(CR
                                .getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
            }
            return email;
    }


        static Cursor getOwner(Context context) {

            String accountName = null;
            Cursor emailCur=null;
            AccountManager accountManager = AccountManager.get(context);
            Account[] accounts = accountManager.getAccountsByType("com.google");
            if (accounts[0].name != null) {
                accountName = accounts[0].name;
                String where = ContactsContract.CommonDataKinds.Email.DATA + " = ?";
                ArrayList<String> what = new ArrayList<String>();
                what.add(accountName);
                Log.v("Got account", "Account " + accountName);
                for (int i = 1; i < accounts.length; i++) {
                    where += " or " + ContactsContract.CommonDataKinds.Email.DATA + " = ?";
                    what.add(accounts[i].name);
                    Log.v("Got account", "Account " + accounts[i].name);
                }
                String[] whatarr = (String[]) what.toArray(new String[what.size()]);
                ContentResolver cr = context.getContentResolver();
                emailCur = cr.query(
                        ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
                        where,
                        whatarr, null);

               /* 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();
                }*/
            }
            return emailCur;
        }
}
于 2015-06-23T13:33:14.277 回答
-2

这对你有很大帮助............

获取手机号码:

如何在真机中获取当前sim卡的手机号?

获取用户名: 如何在 Android 上获取 google 用户名?

尝试在真实设备中运行它......

于 2013-08-23T03:02:26.520 回答