Tuesday, April 23, 2013

How to read android sim contacts and phone contacts separately

The <uses-permission> should be contained in the <manifest> element. See Structure of the Manifest File. So trying putting it before <application>.
<uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.READ_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));
                System.out.println(c.getString(c.getColumnIndex(Contacts._ID)));

                ClsphoneNo.replaceAll("\\D", "");
                ClsPhonename=ClsPhonename.replaceAll("&", "");
                ClsPhonename.replace("|","");
                String ClsPhoneName=ClsPhonename.replace("|","");
                System.out.println("Name: "+ClsPhoneName);
                System.out.println("Phone: "+ClsphoneNo);
            } while(c.moveToNext());
    	}
    	
    	
    	Uri simUri = Uri.parse("content://icc/adn"); 
        Cursor cursorSim = this.getContentResolver().query(simUri,null,null,null,null);
        String ClsSimPhonename = null; 
        String ClsSimphoneNo = 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);

        }
    } catch (Exception ex) {
    	System.out.println(ex.getMessage());
    	ex.printStackTrace();
    }

No comments:

Post a Comment