Cdacians

Cdacians
Cdacians

Tuesday 7 August 2012

Pick contact using intent of Intent.ACTION_PICK, with People.CONTENT_URI


Pick contact using intent of Intent.ACTION_PICK, with People.CONTENT_URI

It's a example to pick from system contact, using intent of Intent.ACTION_PICK, with People.CONTENT_URI. And show how to retrieve the contact in onActivityResult(), using Cursor.

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package com.AndroidPickContact;
 
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Contacts.People;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
 
public class AndroidPickContact extends Activity {
  
 final static int RQS_PICK_CONTACT = 1;
  
 TextView contactName, contactNumber, contactEmail;
  
   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);
       contactName = (TextView)findViewById(R.id.contactname);
       contactNumber = (TextView)findViewById(R.id.contactnumber);
       contactEmail = (TextView)findViewById(R.id.contactemail);
       Button buttonPickContact = (Button)findViewById(R.id.pickcontact);
       buttonPickContact.setOnClickListener(new Button.OnClickListener(){
 
   @Override
   public void onClick(View arg0) {
    // TODO Auto-generated method stub
    Intent intent = new Intent(Intent.ACTION_PICK, People.CONTENT_URI);
    startActivityForResult(intent, RQS_PICK_CONTACT);
   }});
   }
 
 @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  // TODO Auto-generated method stub
  super.onActivityResult(requestCode, resultCode, data);
   
  if(requestCode == RQS_PICK_CONTACT){
   if(resultCode == RESULT_OK){
    Uri contactData = data.getData();
    Cursor cursor =  managedQuery(contactData, null, null, null, null);
    cursor.moveToFirst();
          String name = cursor.getString(cursor.getColumnIndexOrThrow(People.NAME));
          String number = cursor.getString(cursor.getColumnIndexOrThrow(People.NUMBER));
          String email = cursor.getString(cursor.getColumnIndexOrThrow(People.PRIMARY_EMAIL_ID));
          contactName.setText(name);
          contactNumber.setText(number);
          contactEmail.setText(email);
   }
  }
 }
}


?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   >
<TextView
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="@string/hello"
   />
<Button
   android:id="@+id/pickcontact"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="Pick Contact"
   />
<TextView
   android:id="@+id/contactname"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   />
<TextView
   android:id="@+id/contactnumber"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   />
<TextView
   android:id="@+id/contactemail"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   />
</LinearLayout>


akm
www.cdacians.com

No comments:

Post a Comment