Android examples for Account:Contact Number
search And Send Contact Info
//package com.java2s; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import android.app.PendingIntent; import android.content.ContentResolver; import android.content.Context; import android.content.SharedPreferences; import android.database.Cursor; import android.provider.ContactsContract; import android.telephony.PhoneNumberUtils; import android.telephony.SmsManager; import android.util.Log; public class Main { public static final String PREFS_KEY_ACTIVE_SESSION_PHONE_NUMBER = "prefs_key_active_session_phone_number"; public static final String PREFS_KEY_LAST_MESSAGE_RECEIVED = "prefs_key_last_message_received"; public static final String PREFS_KEY_MODE = "prefs_key_mode"; public static final String PREFS_KEY_STAGE = "prefs_key_stage"; public static final String PREFS_KEY_ACTIVE_SESSION_STRING_EXTRA = "prefs_key_active_session_string_extra"; /**/*from w w w.java 2 s.c o m*/ * Example: Yes (as response to get contact dad => Dad: 122121..More? --> Yes ) */ public static final int MODE_YES_MORE = 6; private static String CONTACT_SEARCH_WHERE_CONDITION = ContactsContract.Data.MIMETYPE + " = '" + ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE + "'" + " AND " + ContactsContract.Data.DISPLAY_NAME + " LIKE ?"; private static String[] CONTACT_SEARCH_PROJECTION = { ContactsContract.Data.DISPLAY_NAME, ContactsContract.Data.DATA1, ContactsContract.Data.DATA2 }; private static String CONTACT_SEARCH_SORT_ORDER = ContactsContract.Data.DISPLAY_NAME; public static final int LENGTH_OF_REPLY_SMS_MESSAGE = 160; public static void searchAndSendContactInfo(Context context, String phoneNumber, String searchName) { String replyMessageBody; Cursor resultCursor = searchContact(context, searchName); resultCursor.moveToFirst(); if (resultCursor.isAfterLast() == true) { // No contacts found replyMessageBody = "Sorry, no contact in the name of '" + searchName + "' found."; sendSms(phoneNumber, replyMessageBody); } else { // Contacts found! replyMessageBody = buildContactsReplyMessage(context, phoneNumber, resultCursor); sendSms(phoneNumber, replyMessageBody); } } private static Cursor searchContact(Context context, String searchName) { ContentResolver contentResolver = context.getContentResolver(); String searchArgs = "%" + searchName.trim() + "%"; String[] WHERE_CONDITION_ARGS = { searchArgs }; // ---------- Cursor creation Cursor cursor = contentResolver.query( ContactsContract.Data.CONTENT_URI, CONTACT_SEARCH_PROJECTION, CONTACT_SEARCH_WHERE_CONDITION, WHERE_CONDITION_ARGS, CONTACT_SEARCH_SORT_ORDER); return cursor; } /** * Method to send SMS message * * @param phoneNumber The phone number that the message is to be sent to * @param body content of the SMS message */ public static void sendSms(String phoneNumber, String body) { Log.d("WipiwayUtils", "Sending SMS message - " + body + " ... Phone number - " + phoneNumber); SmsManager sms = SmsManager.getDefault(); try { sms.sendTextMessage(phoneNumber, null, body, null, null); } catch (Exception e) { Log.d("WipiwayController", e.toString()); } // Show in outbox - http://stackoverflow.com/a/3873328/804503 /* * ContentValues values = new ContentValues(); values.put("address", phone); values.put("body", message); getContentResolver().insert(Uri.parse("content://sms/sent"), values); */ } /** * Method to send SMS message with a Pending Intent * * @param phoneNumber The phone number that the message is to be sent to * @param body content of the SMS message * @param sentPI Pending intent that receives broadcast of when message is sent (To write the action into the database if sent successfully) */ public static void sendSms(String phoneNumber, String body, PendingIntent sentPI) { SmsManager sms = SmsManager.getDefault(); try { sms.sendTextMessage(phoneNumber, null, body, sentPI, null); } catch (Exception e) { Log.d("WipiwayController", e.toString()); } } private static String buildContactsReplyMessage(Context context, String phoneNumber, Cursor resultCursor) { boolean isMessageFull = false; boolean isDuplicateNumber = false; StringBuffer replyMessage = new StringBuffer(); StringBuffer remainingReplyMessage = new StringBuffer(); // To track & store for later String previousName = ""; String currentName = ""; List<String> listPhoneNumbers = new ArrayList<String>(); Iterator<String> iteratorListPhoneNumbers; while (resultCursor.isAfterLast() == false) { iteratorListPhoneNumbers = listPhoneNumbers.iterator(); while (iteratorListPhoneNumbers.hasNext()) { if (PhoneNumberUtils.compare(resultCursor.getString(1), iteratorListPhoneNumbers.next())) { isDuplicateNumber = true; // Log.d(TAG, // "duplicate numbers! - " + cursor.getString(1)); } } // --- if two rows have the exact same no if (isDuplicateNumber) { resultCursor.moveToNext(); } else { currentName = resultCursor.getString(0).trim(); if (currentName.contentEquals(previousName)) { if (isMessageFull == false && (replyMessage.length() + resultCursor .getString(1).length()) < (LENGTH_OF_REPLY_SMS_MESSAGE - 7)) { replyMessage.append(", " + resultCursor.getString(1).trim()); } else { isMessageFull = true; remainingReplyMessage.append(", " + resultCursor.getString(1).trim()); } } else { if (isMessageFull == false && (replyMessage.length() + currentName.length() + resultCursor .getString(1).length()) < (LENGTH_OF_REPLY_SMS_MESSAGE - 7)) { replyMessage.append(" " + currentName + ": " + resultCursor.getString(1).trim()); } else { remainingReplyMessage.append(" " + currentName + ": " + resultCursor.getString(1).trim()); isMessageFull = true; } } listPhoneNumbers.add(resultCursor.getString(1).trim()); previousName = currentName; resultCursor.moveToNext(); } isDuplicateNumber = false; } if (isMessageFull) { replyMessage.append("..More?"); // Populate preference so that the app can handle a "Yes" response. setActiveSessionPresent(context, phoneNumber); setActiveSessionMode(context, MODE_YES_MORE); setActiveSessionStringExtra(context, remainingReplyMessage.toString()); } return replyMessage.toString(); } /** * Method to set a new active session * * @param context Context * @param phoneNumber Phone number that is associated with the active session */ public static void setActiveSessionPresent(Context context, String phoneNumber) { resetActiveSessionPresent(context); SharedPreferences prefs = context.getSharedPreferences( context.getPackageName(), Context.MODE_PRIVATE); prefs.edit() .putString(PREFS_KEY_ACTIVE_SESSION_PHONE_NUMBER, phoneNumber).commit(); prefs.edit() .putLong(PREFS_KEY_LAST_MESSAGE_RECEIVED, System.currentTimeMillis()).commit(); } /** * Method to set the new assumed Mode of the active session * * @param context * @param mode The new assumed Mode of the active session */ public static void setActiveSessionMode(Context context, int mode) { SharedPreferences prefs = context.getSharedPreferences( context.getPackageName(), Context.MODE_PRIVATE); prefs.edit().putInt(PREFS_KEY_MODE, mode).commit(); } /** * Method to set the new extra string for active session (example: remaining contacts string) * * @param context * @param mode The new extra string for active session (example: remaining contacts string) */ public static void setActiveSessionStringExtra(Context context, String stringExtra) { SharedPreferences prefs = context.getSharedPreferences( context.getPackageName(), Context.MODE_PRIVATE); prefs.edit() .putString(PREFS_KEY_ACTIVE_SESSION_STRING_EXTRA, stringExtra).commit(); } /** * Method to reset the the Active session information * * @param context */ public static void resetActiveSessionPresent(Context context) { SharedPreferences prefs = context.getSharedPreferences( context.getPackageName(), Context.MODE_PRIVATE); prefs.edit().putString(PREFS_KEY_ACTIVE_SESSION_PHONE_NUMBER, "") .commit(); prefs.edit().putLong(PREFS_KEY_LAST_MESSAGE_RECEIVED, 0).commit(); prefs.edit().putInt(PREFS_KEY_STAGE, 0).commit(); prefs.edit().putInt(PREFS_KEY_MODE, 0).commit(); prefs.edit().putString(PREFS_KEY_ACTIVE_SESSION_STRING_EXTRA, "") .commit(); } }