How to Get All Android Contacts But Without Those Which Are on Sim

How to get all android contacts but without those which are on SIM

This is very easy! :)

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

Is it possible to exclude SIM contacts when using Intent.ACTION_PICK?

No, it's not possible

Unfortunately, it's not possible for now.

To proof it, let's dive into source code of ContanctsListActivity.

Here's an onCreate() method of the Activity. In it, ContactApp reads Intent(ACTION_PICK) we passing to it and handles it respectively:

@Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);

mIconSize = getResources().getDimensionPixelSize(android.R.dimen.app_icon_size);
mContactsPrefs = new ContactsPreferences(this);
mPhotoLoader = new ContactPhotoLoader(this, R.drawable.ic_contact_list_picture);

// Resolve the intent
final Intent intent = getIntent();

// Allow the title to be set to a custom String using an extra on the intent
String title = intent.getStringExtra(UI.TITLE_EXTRA_KEY);
if (title != null) {
setTitle(title);
}

String action = intent.getAction();
String component = intent.getComponent().getClassName();

// When we get a FILTER_CONTACTS_ACTION, it represents search in the context
// of some other action. Let's retrieve the original action to provide proper
// context for the search queries.
if (UI.FILTER_CONTACTS_ACTION.equals(action)) {
mSearchMode = true;
mShowSearchSnippets = true;
Bundle extras = intent.getExtras();
if (extras != null) {
mInitialFilter = extras.getString(UI.FILTER_TEXT_EXTRA_KEY);
String originalAction =
extras.getString(ContactsSearchManager.ORIGINAL_ACTION_EXTRA_KEY);
if (originalAction != null) {
action = originalAction;
}
String originalComponent =
extras.getString(ContactsSearchManager.ORIGINAL_COMPONENT_EXTRA_KEY);
if (originalComponent != null) {
component = originalComponent;
}
} else {
mInitialFilter = null;
}
}

Log.i(TAG, "Called with action: " + action);
mMode = MODE_UNKNOWN;
if (UI.LIST_DEFAULT.equals(action) || UI.FILTER_CONTACTS_ACTION.equals(action)) {
.....
else if (Intent.ACTION_PICK.equals(action)) {
// XXX These should be showing the data from the URI given in
// the Intent.
final String type = intent.resolveType(this);
if (Contacts.CONTENT_TYPE.equals(type)) {
mMode = MODE_PICK_CONTACT;
} else if (People.CONTENT_TYPE.equals(type)) {
mMode = MODE_LEGACY_PICK_PERSON;
} else if (Phone.CONTENT_TYPE.equals(type)) {
mMode = MODE_PICK_PHONE;
} else if (Phones.CONTENT_TYPE.equals(type)) {
mMode = MODE_LEGACY_PICK_PHONE;
} else if (StructuredPostal.CONTENT_TYPE.equals(type)) {
mMode = MODE_PICK_POSTAL;
} else if (ContactMethods.CONTENT_POSTAL_TYPE.equals(type)) {
mMode = MODE_LEGACY_PICK_POSTAL;
}
....
// VERY LONG IF WITH DIFFERENT MODE-SELECTION
....
}
.....
if (mMode == MODE_JOIN_CONTACT) {
setContentView(R.layout.contacts_list_content_join);
} else if (mSearchMode) {
setContentView(R.layout.contacts_search_content);
} else if (mSearchResultsMode) {
setContentView(R.layout.contacts_list_search_results);
} else {
setContentView(R.layout.contacts_list_content);
}

setupListView();
...
}

It's very long method (and I also suggest to check setupListView() method), but pretty straightforward. And, as you can see, there's no parameter you can pass to specify source of contacts you want to pick from. Only thing you can configure here is the particular mMode ContactsApp to use (MODE_PICK_CONTACT, MODE_PICK_PHONE, etc.) - but unfortunately number of possible modes is very limited by 6 and non of them suits you.

(If anyone needs to pass mMode to ContanctsListActivity - use intent's setType() method, for example: intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);)

Workaround

As a workaround - as tiny sunlight's suggested in comments - render non-SIM contacts within the app and pick the one you needed from there.

How to get all android contacts but without those which are on SIM - this link looks like most promising one explaining how to query cursor with all contacts, apart from SIM ones.

I hope, it helps

How to read android sim contacts and phone contacts separately

for Phone contacts

try
{
String[] PROJECTION=new String[] {Contacts._ID,
Contacts.DISPLAY_NAME,
Phone.NUMBER
};

Cursor c=managedQuery(Phone.CONTENT_URI,
PROJECTION, null, null, null);
if (c.moveToFirst()) {
String ClsPhonename = null;
String ClsphoneNo = null;

do
{
ClsPhonename = c.getString(c.getColumnIndex(Contacts.DISPLAY_NAME));
ClsphoneNo = c.getString(c.getColumnIndex(Phone.NUMBER));

ClsphoneNo.replaceAll("\\D", "");
ClsPhonename=ClsPhonename.replaceAll("&", "");
ClsPhonename.replace("|","");
String ClsPhoneName=ClsPhonename.replace("|","");

}

} while(c.moveToNext());
}

for sim contacts

String ClsSimPhonename = null;
String ClsSimphoneNo = null;

    Uri simUri = Uri.parse("content://icc/adn"); 
Cursor cursorSim = this.getContentResolver().query(simUri,null,null,null,null);

while (cursorSim.moveToNext())
{
ClsSimPhonename =cursorSim.getString(cursorSim.getColumnIndex("name"));
ClsSimphoneNo = cursorSim.getString(cursorSim.getColumnIndex("number"));
ClsSimphoneNo.replaceAll("\\D","");
ClsSimphoneNo.replaceAll("&", "");
ClsSimPhonename=ClsSimPhonename.replace("|","");
System.out.println("SimContacts"+ClsSimPhonename);
System.out.println("SimContactsNo"+ClsSimphoneNo);
dts.createDatabse("MyCellFamily",getApplicationContext());

}
}
catch(Exception e)
{
e.printStackTrace();
}

How to get contacts from contact list in Android? using Contact Provider

It is obvious to get duplicate contacts when you fetch them. Since, same contact (same number, different name or different number, same name) might be stored in either in google contacts, phone contacts or in sim contacts.

In order to eliminate duplicates in your application, use local database (SQLite provided by android) and make phone number as unique, no duplicates will be stored.

For more information refer this post How to get all android contacts but without those which are on SIM

Following Dalma Racz comment, will also lead you to the solution.



Related Topics



Leave a reply



Submit