sim card - How to get all android contacts but without those which are on SIM - Stack Overflow:
i used the next function to get the contacts with phone, include/exclude from sim , in one query
the function is part of big program so you might need to change /add it
// the idea is get the contacts with the phone first
public static List<Contact> getContacts(Context context,
boolean favoritesOnly, boolean removeSimContacts) {
boolean withEmail = true;
List<Contact> contactsList = null;
HashMap<String, Contact> contactsMap = new HashMap<String, Contact>();
String starred = favoritesOnly ? " AND starred=1 " : "";
String sNOSimContacs = removeSimContacts ? "%sim%": "%%";
String filter = ContactsContract.Contacts.HAS_PHONE_NUMBER + "=1 " + starred + " AND " ;
if(removeSimContacts)
filter += RawContacts.ACCOUNT_TYPE + " NOT LIKE ? AND ";
else
filter += RawContacts.ACCOUNT_TYPE + " LIKE ? AND ";
filter += Data.MIMETYPE + "=?";
Cursor cursor = null;
try {
String[] projection = new String[] {
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts.HAS_PHONE_NUMBER,
ContactsContract.Contacts._ID,
ContactsContract.Contacts.STARRED,
ContactsContract.CommonDataKinds.Phone.DATA,
ContactsContract.CommonDataKinds.Phone.TYPE,
ContactsContract.CommonDataKinds.Phone.LABEL,
ContactsContract.RawContacts.CONTACT_ID};
cursor = context.getContentResolver().query(Data.CONTENT_URI,
projection,
filter, new String[] { sNOSimContacs,Phone.CONTENT_ITEM_TYPE},
ContactsContract.Contacts.DISPLAY_NAME);
while (cursor != null && cursor.moveToNext()) {
String contactId = cursor.getString(cursor
.getColumnIndex(ContactsContract.RawContacts.CONTACT_ID));
// if we dont have this contact add it
// if the user have multi numbers we get the same contact here
if(!contactsMap.containsKey(contactId)){
Contact contact;
if(Const.IS_USE_SHARED_PREF_ONLY)
contact = ContactProviderAlternative.getContact(context,
Integer.parseInt(contactId));
else
contact = ContactProvider.getContact(context,
Integer.parseInt(contactId));
// if (favoritesOnly)
// Log.d("Info", "pos: " + contact.getContactPosition());
boolean isFav = (cursor.getShort(cursor
.getColumnIndex(ContactsContract.Contacts.STARRED)) == 1);
contact.setFavorite(isFav);
contact.setContactId(contactId);
contact.setContactName(cursor.getString(cursor
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)));
contact.setContactPhoto(Utils.openPhoto(context,
Long.parseLong(contactId), false));
contact.setContactUri(Integer.parseInt(contactId));
contact.setContactPhone(cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DATA)));
int type = cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
contact.setContactPhoneType(type == 1 ? Const.NEW_CONTACT_PHONE_TYPE_LANDLINE : Const.NEW_CONTACT_PHONE_TYPE_MOBILE);
contactsMap.put(contactId,contact);
}else{
// we have this conatct lets update his phone the latest one we get
Contact contact = contactsMap.get(contactId);
contact.setContactPhone(cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DATA)));
int type = cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
contact.setContactPhoneType(type == 1 ? Const.NEW_CONTACT_PHONE_TYPE_LANDLINE : Const.NEW_CONTACT_PHONE_TYPE_MOBILE);
}
}
if(cursor != null){
cursor.close();
cursor = null;
}
contactsList = new ArrayList<Contact>(contactsMap.values());
if(withEmail){
String[] projectionEmail = new String[] {
ContactsContract.CommonDataKinds.Email.DATA,
ContactsContract.CommonDataKinds.Email.TYPE,
ContactsContract.CommonDataKinds.Email.LABEL
};
String where= Data.CONTACT_ID + " = ? AND " + Data.MIMETYPE + "=?";
for(Contact c : contactsList){
//email & phone
String[] params = new String[]{c.getContactId(), Email.CONTENT_ITEM_TYPE};
cursor = context.getContentResolver().query(
Data.CONTENT_URI,
projectionEmail,
where,
params,
null);
while(cursor != null && cursor.moveToNext()) {
c.setContactEmail(cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)));
}
if(cursor != null){
cursor.close();
cursor = null;
}
}
}
}
catch (Exception e) {
e.printStackTrace();
}finally{
if(cursor != null)
cursor.close();
}
return contactsList;
}
Yaniv Tzannay
This Blog is first of all memos for me!! From time to time I get exposed to new and cool stuff for developers and architects ,most of the time its a solution for a problem or tips or tricks, so, I would like to track my memos and share it with you, it can save your time and nervous, I hope you will enjoy it - by Yaniv Tzanany.
Search This Blog
Thursday, July 18, 2013
Sunday, July 14, 2013
Android icons size for tab
for mdpi use - 32x32 pixel
for hdpi use - 48x48 pixel
for xhdpi use - 64x64 pixel
more stuff could be found here
please note when you put an image on xhdpi - android know how to scale down your image ...
yaniv tzanany
for hdpi use - 48x48 pixel
for xhdpi use - 64x64 pixel
more stuff could be found here
Iconography
http://developer.android.com/design/style/iconography.htmlplease note when you put an image on xhdpi - android know how to scale down your image ...
yaniv tzanany
Thursday, July 11, 2013
Android change some system settings
i was looking for some info about chnaging system setttings , here arte some pointers for links that might help you .
The major class is : /Settings.Secure
http://developer.android.com/reference/android/provider/Settings.Secure.html
allowed adb insatll
http://stackoverflow.com/questions/11985251/how-to-access-device-settings-programmatically
Settings.Secure.putInt(getActivity().getContentResolver(),Settings.Secure.ADB_ENABLED, 1);
How to change programmatically a global setting like 'haptic feedback'?
http://stackoverflow.com/questions/7696934/how-to-change-programmatically-a-global-setting-like-haptic-feedback
Settings.System.putInt(getContentResolver(), Settings.System.HAPTIC_FEEDBACK_ENABLED, 0);
Control default auto rotate screen in my application
http://stackoverflow.com/questions/9718317/control-default-auto-rotate-screen-in-my-application
public static void setAutoOrientationEnabled(ContentResolver resolver, boolean enabled)
{
Settings.System.putInt(resolver, Settings.System.ACCELEROMETER_ROTATION, enabled ? 1 : 0);
}
How to change screen timeout programmatically?
http://stackoverflow.com/questions/10748861/how-to-change-screen-timeout-programmatically
android.provider.Settings.System.putInt(getContentResolver(),
Settings.System.SCREEN_OFF_TIMEOUT, time);
turn-the-volume-to-max
http://stackoverflow.com/questions/15670524/how-to-turn-the-volume-to-max-programmatically-on-android
change keyboard
http://stackoverflow.com/questions/6901097/change-input-method-of-android-device-programatically-android
not possible to change keyboard
GPS - on off
http://stackoverflow.com/questions/4721449/enable-gps-programatically-like-tasker
http://stackoverflow.com/questions/8983772/turn-on-location-providers-programmatically-in-android
Monday, July 1, 2013
Entity frameworks notes
if you get such error while serialize your entity ".......System.Data.Entity.DynamicProxies is not expected."
after creating your entity context (generated class derived from DbContext) the next line:
base.Configuration.ProxyCreationEnabled = false;
to solve the error of
DataContractJsonSerializer "reference tracking is disabled"
i remove some of my association i created to avoid back reference
Loading Related Entities - http://msdn.microsoft.com/en-us/data/jj574232.aspx
links for EE framework
http://blogs.msdn.com/b/adonet/archive/2011/01/31/using-dbcontext-in-ef-feature-ctp5-part-6-loading-related-entities.aspx
To get the SQL query that generated by db entity use:
var sql = ((System.Data.Entity.Infrastructure.DbQuery<tbl_artists>)query).ToString();
if its object:
var sql = ((System.Data.Objects.ObjectQuery)result).ToTraceString();
after creating your entity context (generated class derived from DbContext) the next line:
base.Configuration.ProxyCreationEnabled = false;
to solve the error of
DataContractJsonSerializer "reference tracking is disabled"
i remove some of my association i created to avoid back reference
Loading Related Entities - http://msdn.microsoft.com/en-us/data/jj574232.aspx
links for EE framework
http://blogs.msdn.com/b/adonet/archive/2011/01/31/using-dbcontext-in-ef-feature-ctp5-part-6-loading-related-entities.aspx
To get the SQL query that generated by db entity use:
var sql = ((System.Data.Entity.Infrastructure.DbQuery<tbl_artists>)query).ToString();
if its object:
var sql = ((System.Data.Objects.ObjectQuery)result).ToTraceString();
Basic LINQ Query Operations (C#)
Subscribe to:
Posts (Atom)