Please read the SDK documentation. There is in fact code to do exactly this.

On Fri, Sep 17, 2010 at 8:22 AM, mishra <[email protected]> wrote:

> hello all
>
>     I am trying to access the phone number but not success , I can
> access name and id from the original phone book to my application but
> not phone number the code of accessing the name and id is..
>
>
>
>
> package org.mobilesoftphone.ui;
>
> import android.app.Activity;
> import android.content.Intent;
> import android.database.Cursor;
> import android.net.Uri;
> import android.os.Bundle;
> import android.provider.BaseColumns;
> import android.provider.ContactsContract;
> import android.view.Menu;
> import android.view.MenuItem;
> import android.view.View;
> import android.widget.AdapterView;
> import android.widget.EditText;
> import android.widget.ListView;
> import android.widget.SimpleCursorAdapter;
>
> public class ContactManager extends Activity {
>
>        public static String abcd = "org.mobilesoftphone.ui";
>        private static final int menu_aboutUs = 1;
>        private static final int menu_exit = 2;
>
>        private static final int menu_option = 0;
>
>        public static final String TAG = "ankush";
>        public Cursor cursor;
>        private ListView mContactList;
>
>        private boolean mShowInvisible;
>        private ListView.OnItemClickListener onListClick = new
> ListView.OnItemClickListener() {
>                public void onItemClick(AdapterView<?> mAdapterView, View v,
>                                int position, long id) {
>                        // TODO Auto-generated method stub
>
>                        String callno = ((Cursor)
> mContactList.getAdapter().getItem(
>                                        position)).getString(1);
>
>                        Intent mainformintent = new
> Intent(ContactManager.this,
>                                        mainform.class);
>                        String name = "org.mobilesoftphone.callstring";
>                        mainformintent.putExtra(name, callno);
>                        ContactManager.this.startActivity(mainformintent);
>                        finish();
>                }
>        };
>
>        public EditText phonetext;
>        public void aboutus() {
>
>        }
>
>        public void exit() {
>                finish();
>
>        }
>
>        /**
>         * Obtains the contact list for the currently selected account.
>         *
>         * @return A cursor for for accessing the contact list.
>         */
>        private Cursor getContacts() {
>                // Run query
>                Uri uri = ContactsContract.Contacts.CONTENT_URI;
>                String[] projection = new String[] { BaseColumns._ID,
>                                ContactsContract.Contacts.DISPLAY_NAME };
>                String selection =
> ContactsContract.Contacts.IN_VISIBLE_GROUP + " =
> '"
>                                + (mShowInvisible ? "0" : "1") + "'";
>                String[] selectionArgs = null;
>                String sortOrder = ContactsContract.Contacts.DISPLAY_NAME
>                                + " COLLATE LOCALIZED ASC";
>
>                return managedQuery(uri, projection, selection,
> selectionArgs,
>                                sortOrder);
>        }
>
>        /**
>         * Called when the activity is first created. Responsible for
> initializing
>         * the UI.
>         */
>        @Override
>        public void onCreate(Bundle savedInstanceState) {
>
>                super.onCreate(savedInstanceState);
>                setContentView(R.layout.phonebook);
>
>                mContactList = (ListView) findViewById(R.id.contactList);
>
>                // Initialize class properties
>                mShowInvisible = false;
>
>                // Register handler for UI elements
>
>                // Populate the contact list
>                populateContactList();
>
>        }
>
>        @Override
>        public boolean onCreateOptionsMenu(Menu menu) {
>                // TODO Auto-generated method stub
>                menu.add(0, menu_option, 0,
> "Setting").setIcon(R.drawable.option);
>
>                menu.add(0, menu_aboutUs, 0, "About
> Us").setIcon(R.drawable.abcd);
>                menu.add(0, menu_exit, 0, "Exit").setIcon(R.drawable.exit);
>
>                // return super.onCreateOptionsMenu(menu);
>                return true;
>        }
>        @Override
>        protected void onDestroy() {
>                super.onDestroy();
>
>        }
>
>        @Override
>        public boolean onOptionsItemSelected(MenuItem item) {
>                // TODO Auto-generated method stub
>
>                switch (item.getItemId()) {
>                case menu_option:
>                        option();
>                        return true;
>                case menu_aboutUs:
>                        aboutus();
>                        return true;
>                case menu_exit:
>                        exit();
>                        return true;
>                }
>                return false;
>
>        }
>
>        @Override
>        public void onPause() {
>
>                super.onPause();
>        }
>
>        @Override
>        public void onRestart() {
>
>                super.onRestart();
>                setContentView(R.layout.phonebook);
>
>                mContactList = (ListView) findViewById(R.id.contactList);
>
>                // Initialize class properties
>                mShowInvisible = false;
>
>                // Register handler for UI elements
>
>                // Populate the contact list
>                populateContactList();
>
>        }
>
>        @Override
>        public void onResume() {
>                super.onResume();
>                setContentView(R.layout.phonebook);
>
>                mContactList = (ListView) findViewById(R.id.contactList);
>
>                // Initialize class properties
>                mShowInvisible = false;
>
>                // Register handler for UI elements
>
>                // Populate the contact list
>                populateContactList();
>
>        }
>
>        @Override
>        public void onStart() {
>
>                super.onStart();
>                setContentView(R.layout.phonebook);
>
>                mContactList = (ListView) findViewById(R.id.contactList);
>
>                // Initialize class properties
>                mShowInvisible = false;
>
>                // Register handler for UI elements
>
>                // Populate the contact list
>                populateContactList();
>
>        }
>
>        @Override
>        protected void onStop() {
>                super.onStop();
>
>        }
>
>        public void option() {
>
>        }
>
>        /**
>         * Populate the contact list based on account currently selected in
> the
>         * account spinner.
>         */
>        private void populateContactList() {
>                // Build adapter with contact entries
>                cursor = getContacts();
>                String[] fields = new String[] { BaseColumns._ID,
>                                ContactsContract.Data.DISPLAY_NAME };
>                SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
>                                R.layout.contact_entry, cursor, fields, new
> int[] {
>                                                R.id.contactEntryid,
> R.id.contactEntryText });
>                mContactList.setAdapter(adapter);
>                mContactList.setOnItemClickListener(onListClick);
>        }
>
> }
>
>
>
> please tell me how to access the phone number from the phone book...
>  thanks a lot
>
> --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected]<android-developers%[email protected]>
> For more options, visit this group at
> http://groups.google.com/group/android-developers?hl=en
>

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to