【问题标题】:Finding account nature of a contact group?查找联系人组的帐户性质?
【发布时间】:2011-01-06 10:48:49
【问题描述】:

我正在开发一个应用程序,其中需要查找联系人组的性质意味着它是 google 组、电话组还是 sim 组。如何找到它。请建议我怎么做。 提前致谢。

【问题讨论】:

    标签: android


    【解决方案1】:

    下面的代码打印联系人姓名和类型。我还没有优化它,它会打印多条记录,但我想你会知道该怎么做。

    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);
    }
    }
    

    【讨论】:

      【解决方案2】:

      我有同样的问题,你提到我是这样解决的

      ArrayList<GroupNameDetails> stateList = new ArrayList<GroupNameDetails>();
            final String[] GROUP_PROJECTION = new String[] 
                      {
                          ContactsContract.Groups._ID, ContactsContract.Groups.TITLE,   ContactsContract.Groups.ACCOUNT_TYPE//this line will do the trick
                      };
                  Cursor cursor = getContentResolver().query(ContactsContract.Groups.CONTENT_URI, GROUP_PROJECTION, null,
                          null, ContactsContract.Groups.TITLE);
                  while (cursor.moveToNext()) {
                      String accountname=cursor.getString(cursor.getColumnIndex(ContactsContract.Groups.ACCOUNT_TYPE));
                      Toast.makeText(getBaseContext(), accountname, Toast.LENGTH_LONG).show();// and it will display group type
                      String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Groups._ID));
                      Log.v("Test", id);
                      //ContactsContract.Groups.ACCOUNT_NAME
      
                      String gTitle = (cursor.getString(cursor.getColumnIndex(ContactsContract.Groups.TITLE)));
                      if(favGroupName.contains(gTitle)==false)
                      {
                      favGroupId.add(id);
                      favGroupName.add(gTitle);
      
                      GroupNameDetails _states = new GroupNameDetails(Long.parseLong(id),gTitle, false);
                      stateList.add(_states);
                      }
                      Log.v("Test", gTitle);
                      if (gTitle.contains("Favorite_")) {
                          gTitle = "Favorites";
      
                      }
      
                  }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-04-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-02-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多