今天原本想随便写个ContentProvider demo玩玩的。。。但万万没想到一个简单的被我弄了4个小时才弄出来。。。关于ContentProvider今天就到这里把明天再去弄。。。以下代码经过测试
public class MainActivity extends Activity {
protected static final int PICK = 0;
private static final String TAG =
"MainActivity";
private TextView textView_name;
private TextView
textView_phone;
private Button button;
@Override
protected void
onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView_name=(TextView)
findViewById(R.id.TextView_name);
textView_phone=(TextView)
findViewById(R.id.TextView_phone);
button=(Button)
findViewById(R.id.button);
button.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
Intent
intent=new
Intent();
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.setType("vnd.android.cursor.item/phone");
startActivityForResult(intent,
PICK);
}
});
}
@Override
protected void onActivityResult(int requestCode, int
resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode,
data);
if(requestCode==PICK)
{
if(resultCode==Activity.RESULT_OK)
{
Uri
uri=data.getData();
CursorLoader cursorLoader=new
CursorLoader(this,uri,null,null,null,null);
Cursor
cursor=cursorLoader.loadInBackground();
if(cursor.moveToFirst())
{
String
contactId=cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
String
name=cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
String
phoneNumber="此联系人暂未输入电话号码";
Log.i(TAG, "联系人:"+contactId);
Cursor
phones=getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone._ID+"="+contactId,
//这个地方一定要注意是_ID不是CONTACT_ID
null,
null);
Log.i(TAG,
"数据总数为:"+phones.getCount());
if(phones.moveToFirst())
{
phoneNumber=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
phones.close();
textView_name.setText(name);
textView_phone.setText(phoneNumber);
}
cursor.close();
}
}
}}
Android通过ContentProvider取得电话本的数据,布布扣,bubuko.com
Android通过ContentProvider取得电话本的数据
原文:http://www.cnblogs.com/Toad520/p/3725913.html