在Android中使用getContentResolver()。query()获取联系号码

时间:2014-11-12 05:05:53

标签: android android-contacts android-contentresolver android-query

我试图使用getContentResolver()。query()方法获取联系号码。但它没有得到任何数字。这是我的代码和应用程​​序文件。

活动类

    abc = (Button) findViewById(R.id.button);
    abc.setOnClickListener(new View.OnClickListener() {
        @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
        @Override
        public void onClick(View v) {
            ContentResolver contentResolver = getContentResolver();
            Cursor cursor = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
            try {
                if (cursor.getCount() > 0) {
                    while (cursor.moveToNext()) {
                        String name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                        String contact_Id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
                        String Numbers = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                        Cursor cursor_phone = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                                ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?" , new String[]{contact_Id}, null);

                        while (cursor_phone.moveToNext()) {
                            String phNumber = cursor_phone.getString(cursor_phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                            Numbers = phNumber;
                            int PHONE_TYPE = cursor_phone.getInt(cursor_phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));

                            switch (PHONE_TYPE) {
                                case ContactsContract.CommonDataKinds.Phone.TYPE_HOME:
                                    Numbers += "Home" + phNumber + "\n";
                                    break;
                                case ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE:
                                    Numbers += "Mobile" + phNumber + "\n";
                                    break;
                                case ContactsContract.CommonDataKinds.Phone.TYPE_WORK:
                                    Numbers += "Work" + phNumber + "\n";
                                    break;
                            }
                        }
                        Toast toast = Toast.makeText(Context, name + Numbers, Toast.LENGTH_SHORT);
                        toast.show();

                    }

                } else {
                    Toast toast = Toast.makeText(a, "Nothing", Toast.LENGTH_SHORT);
                    toast.show();
                }
            }
            catch (Exception Ex)
            {
                Log.d(Ex.toString(),"");
            }

        }

    });

构建Gradle文件

apply plugin: 'com.android.application'    
android {
    compileSdkVersion 20
    buildToolsVersion "20.0.0"

    defaultConfig {
        applicationId "com.metrolixsolutions.contactgetter"
        minSdkVersion 14
        targetSdkVersion 20
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}    

它没有任何数字到 cursor_phone

1 个答案:

答案 0 :(得分:0)

您不需要第二个查询

    abc = (Button) findViewById(R.id.button);
abc.setOnClickListener(new View.OnClickListener() {
    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public void onClick(View v) {
        ContentResolver contentResolver = getContentResolver();
        Cursor cursor = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
        try {
            if (cursor.getCount() > 0) {
                while (cursor.moveToNext()) {
                    String name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                    String contact_Id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)); // should be ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                    // this is the phone number
                    String Numbers = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    // dont do a toast inside a loop, try Log.d
                    // you will have to import Log 
                    Log.d("XYZ", "contact id " + contact_id + " number " + Numbers);
                    Toast toast = Toast.makeText(Context, name + Numbers, Toast.LENGTH_SHORT);
                    toast.show();

                }

            } else {
                Toast toast = Toast.makeText(a, "Nothing", Toast.LENGTH_SHORT);
                toast.show();
            }
        }
        catch (Exception Ex)
        {
            Log.d(Ex.toString(),"");
        }

    }

});