query Super Primary Phone from Contact - Android Account

Android examples for Account:Contact

Description

query Super Primary Phone from Contact

Demo Code

/*//from   ww  w.  java2  s  .  com
 * Copyright (C) 2009 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
//package com.java2s;

import android.content.ContentResolver;
import android.content.ContentUris;

import android.database.Cursor;

import android.net.Uri;

import android.provider.ContactsContract.CommonDataKinds.Phone;

import android.provider.ContactsContract.Contacts;
import android.provider.ContactsContract.Data;

public class Main {
    public static String querySuperPrimaryPhone(ContentResolver cr,
            long contactId) {
        Cursor c = null;
        String phone = null;
        try {
            Uri baseUri = ContentUris.withAppendedId(Contacts.CONTENT_URI,
                    contactId);
            Uri dataUri = Uri.withAppendedPath(baseUri,
                    Contacts.Data.CONTENT_DIRECTORY);

            c = cr.query(dataUri, new String[] { Phone.NUMBER },
                    Data.MIMETYPE + "=" + Phone.MIMETYPE + " AND "
                            + Data.IS_SUPER_PRIMARY + "=1", null, null);
            if (c != null && c.moveToFirst()) {
                // Just return the first one.
                phone = c.getString(0);
            }
        } finally {
            if (c != null) {
                c.close();
            }
        }
        return phone;
    }
}

Related Tutorials