0

我是Android新手,请帮我解决我的问题,这是我的问题....

我正在开发一个用于检索联系人的应用程序,但在我的应用程序中,我需要检索服务中的联系人,并且该服务需要每 5 分钟调用一次……在活动中,我有可能直接检索联系人……我试过了用于检索服务中的联系人,但我找不到任何解决方案..

我们有没有机会请告诉我,还有其他方法可以在服务中检索联系人吗?

4

1 回答 1

0

请参阅此代码片段以检索联系人......

Cursor cursor = mContentResolver.query(
   RawContacts.CONTENT_URI,
   new String[]{RawContacts._ID,RawContacts.ACCOUNT_TYPE},
   RawContacts.ACCOUNT_TYPE + " <> 'com.anddroid.contacts.sim' "
    + " AND " + RawContacts.ACCOUNT_TYPE + " <> 'com.google' " //if you don't want to google contacts also
   ,
   null,
   null);

这是一个完整的程序给你............

package com.example.android.contactmanager;
import android.app.Activity;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.RawContacts;
import android.util.Log;

public final class ContactManager extends Activity{

/**
 * Called when the activity is first created. Responsible for initializing the UI.
 */
@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    printContactList();
}

/**
 * Print contact data in logcat.
 * SIM : Account_Type = com.anddroid.contacts.sim
 * Phone : Depends on the manufacturer e.g For HTC : Account_Type = com.htc.android.pcsc
 * Google : Account_Type = com.google
 */
private void printContactList() {
    Cursor cursor = getContacts();
    cursor.moveToFirst();
    while (cursor.isAfterLast() == false) {
        Log.d("Display_Name", cursor.getString(cursor.getColumnIndex(ContactsContract.Data.DISPLAY_NAME)));
        Log.d("Account_Type", cursor.getString(cursor.getColumnIndex(RawContacts.ACCOUNT_TYPE)));
        cursor.moveToNext();

    }
}

/**
 * Obtains the contact list for the currently selected account.
 *
 * @return A cursor for for accessing the contact list.
 */
private Cursor getContacts()
{
    // Run query
    Uri uri = ContactsContract.Data.CONTENT_URI;
    String[] projection = new String[] {
            ContactsContract.Contacts._ID,
            ContactsContract.Contacts.DISPLAY_NAME,
            RawContacts.ACCOUNT_TYPE
    };
    String[] selectionArgs = null;
    String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";

    return managedQuery(uri, projection, null, selectionArgs, sortOrder);
}
}
于 2012-09-29T08:07:33.790 回答