postheadericon Android Read Phone Contacts Example

Read contact from Phone book  using Content Provider

Ex12Activity.java

package com.bipin.rupadiya.Ex12;



import android.app.Activity;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract.PhoneLookup;
import android.widget.ArrayAdapter;
import android.widget.ListView;



public class Ex12Activity extends Activity {
ListView lvContacts;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        lvContacts = (ListView) findViewById(R.id.lvContacts);
       
       
        Cursor c = managedQuery(Uri.parse("content://contacts/phones"), null, null, null, null);
        String names[] = new String[ c.getCount() ];

        int i=0;
        while (c.moveToNext())
        names[i++] = c.getString(c.getColumnIndex(PhoneLookup.DISPLAY_NAME)) + "\n" + c.getString(c.getColumnIndex(PhoneLookup.NUMBER));

       
        ArrayAdapter<String> adpt = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, names);
        lvContacts.setAdapter(adpt);
    }
}



main.xml

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <ListView android:layout_x="26dp" android:layout_y="34dp" android:layout_width="270dp" android:layout_height="425dp" android:id="@+id/lvContacts"></ListView>
</AbsoluteLayout>




AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.bipin.rupadiya.Ex12"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="8" />
    <uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name="com.bipin.rupadiya.Ex12.Ex12Activity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
</manifest>



 

1 comments:

Unknown said...

Nice post sir.....

Total Pageviews

© BipinRupadiya.com. Powered by Blogger.