Android development: select a contact and return a phone number

Source: Internet
Author: User


When you contact this tutorial, the method used to select a contact is to read the contact from the database, and then display it with listview. After selecting the contact, the mobile phone number is returned,

In this way, when you click to select a contact, it takes time to load and the displayed contacts do not seem to be full,

In short, it is not good. You do not have to process the interface by calling the system contact.

The results are good:

MainActivity:

package jason.pickcontact;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {
Button pick;
TextView show;

@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_main);
pick = (Button) findViewById (R.id.pick);
show = (TextView) findViewById (R.id.show);
pick.setOnClickListener (new OnClickListener () {

@Override
public void onClick (View v) {
// TODO Auto-generated method stub
Intent intent = new Intent (Intent.ACTION_PICK,
ContactsContract.Contacts.CONTENT_URI);
MainActivity.this.startActivityForResult (intent, 1);
}
});
}

@Override
protected void onActivityResult (int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult (requestCode, resultCode, data);
switch (requestCode) {
case 1:
if (resultCode == RESULT_OK) {
Uri contactData = data.getData ();
Cursor cursor = managedQuery (contactData, null, null, null,
null);
cursor.moveToFirst ();
String num = this.getContactPhone (cursor);
show.setText ("The selected mobile phone number is:" + num);
}
break;

default:
break;
}
}

private String getContactPhone (Cursor cursor) {
// TODO Auto-generated method stub
int phoneColumn = cursor
.getColumnIndex (ContactsContract.Contacts.HAS_PHONE_NUMBER);
int phoneNum = cursor.getInt (phoneColumn);
String result = "";
if (phoneNum> 0) {
// Get the ID number of the contact
int idColumn = cursor.getColumnIndex (ContactsContract.Contacts._ID);
String contactId = cursor.getString (idColumn);
// Cursor to get contact phone
Cursor phone = getContentResolver (). Query (
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "="
+ contactId, null, null);
if (phone.moveToFirst ()) {
for (;! phone.isAfterLast (); phone.moveToNext ()) {
int index = phone
.getColumnIndex (ContactsContract.CommonDataKinds.Phone.NUMBER);
int typeindex = phone
.getColumnIndex (ContactsContract.CommonDataKinds.Phone.TYPE);
int phone_type = phone.getInt (typeindex);
String phoneNumber = phone.getString (index);
result = phoneNumber;
// switch (phone_type) {// Please see the note below
// case 2:
// result = phoneNumber;
// break;
//
// default:
// break;
//}
}
if (! phone.isClosed ()) {
phone.close ();
}
}
}
return result;
}

@Override
public boolean onCreateOptionsMenu (Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater (). inflate (R.menu.main, menu);
return true;
}

}

For some questions about phone_type, if the mobile phone number storage type is different in the contact, phone_type is also different. Generally, the type in the mobile phone column is 2,

However, I found that some of the contacts cannot be selected. I found that many contacts do not store their numbers in the mobile phone column. Instead, they are stored as phones or other types,

I stored various types of phone tests and found that my mobile phone is a miui system, the TYPE in the mobile phone column is 2, and the TYPE in the residential apartment is 3 and 1, the types of the switchboard are 12 and 7, and the others are 7.

Using the native system on the simulator, we found that mobile is 2, home is 1, work is 3, and other is 7.

There are some differences between different systems. After all, there have been changes, but the mobile phone number is 2, which should be okay. Because of the incorrect storage type, I commented out the correct code, you can use it without determining the type.

In terms of standards, it is right to add type 2.

I have no idea about the complex relationships in the contact database. Put it first.

Author: jason0539

Weibo: http://weibo.com/2553717707

Blog: http://blog.csdn.net/jason0539 (reprinted please explain the source)


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.