Sunday, 1 September 2013

Read Device Contacts Demo ( Content Provider Demo ) ( Acess another DB )

Demo: 
Devices => 2.3 & 4.2

1) MainActivity.java:

package com.venky.readdevicecontactsdemo;
import java.util.ArrayList; 
import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends Activity {

       ListView listView = null;

       @Override
       protected void onCreate(Bundle savedInstanceState) {
             super.onCreate(savedInstanceState);
             setContentView(R.layout.activity_main);

             listView = (ListView) findViewById(R.id.listView);

             ArrayList<String> contacts = new ArrayList<String>();

             ArrayAdapter<String> myAdapter = new ArrayAdapter<String>(this,
                           android.R.layout.simple_list_item_1, contacts);
             listView.setAdapter(myAdapter);

             ContentResolver con = getContentResolver();

             Cursor c = con.query(ContactsContract.Contacts.CONTENT_URI, null, null,
                           null, null);
             if (c.getCount() > 0) {

                    while (c.moveToNext()) {

                           String id = c.getString(c
                                        .getColumnIndex(ContactsContract.Contacts._ID));
                           String name = c
                            .getString(c
                            .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

                           String phoneNumber = null;
                           ArrayList<String> phArray = new ArrayList<String>();

                           int hasPhone = c
                            .getInt(c
                      .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));

                           if (hasPhone == 1) {

                                 Cursor phCursor = con.query(
                  ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                  null,
                  ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                  + "=" + id, null, null);

                                 while (phCursor.moveToNext()) {

                                        phoneNumber = phCursor
                                          .getString(phCursor
                   .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                                        phArray.add(phoneNumber);
                                 }
                                 phCursor.close();
                           }

                           contacts.add(id + ". " + name + "\n" + phArray);
                    }
             }
       }
}

2) activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:android1="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ListView
        android1:id="@+id/listView"
        android1:layout_width="match_parent"
        android1:layout_height="wrap_content" />

</LinearLayout>

3) Add this Permission into your manifest.xml:

  <uses-permission android:name="android.permission.READ_CONTACTS"/>

4) Download this Project Click Here

No comments:

Post a Comment