/** * Query all contact name from contact table. * * @return all contact name */ public String[] getContactsNames() { ArrayList<String> contactsList = new ArrayList<String>(); String columnsInbox[] = new String[] {Contacts.DISPLAY_NAME}; Cursor cursor = null; try { cursor = mContext .getContentResolver() .query(CONTACTS_URI, columnsInbox, null, null, Contacts.SORT_KEY_PRIMARY); if (cursor != null && cursor.moveToFirst()) { int count = cursor.getCount(); Log.d(TAG, "[getContactsNames] cursor count = " + count); String contact; for (int i = 0; i < count; i++) { int columnIndex = cursor.getColumnIndex(Contacts.DISPLAY_NAME); if (columnIndex >= 0) { contact = cursor.getString(columnIndex); if (!TextUtils.isEmpty(contact)) { contactsList.add(contact); } } cursor.moveToNext(); } } } finally { if (cursor != null) { cursor.close(); } } return contactsList.toArray(new String[contactsList.size()]); }
@Override public void handleMessage(Message msg) { super.handleMessage(msg); Log.d(TAG, "[handleMessage]msg.what = " + msg.what); switch (msg.what) { case MSG_GET_CONTACTS_NAME: sendToMainHandler(); break; default: break; } }
private void sendToMainHandler() { Log.d(TAG, "[sendToMainHandler]..."); if (mMainHandler.hasMessages(VoiceCommandListener.ACTION_VOICE_CONTACTS_NAME)) { mMainHandler.removeMessages(VoiceCommandListener.ACTION_VOICE_CONTACTS_NAME); } // Query database after remove the contacts msg of main handler String[] contactsNames = getContactsNames(); VoiceMessage message = new VoiceMessage(); message.mMainAction = VoiceCommandListener.ACTION_MAIN_VOICE_CONTACTS; message.mSubAction = VoiceCommandListener.ACTION_VOICE_CONTACTS_NAME; Bundle bundle = DataPackage.packageSendInfo(contactsNames, null); message.mExtraData = bundle; Message msg = mMainHandler.obtainMessage(); msg.what = VoiceCommandListener.ACTION_MAIN_VOICE_CONTACTS; msg.obj = message; mMainHandler.sendMessage(msg); }