标签:des android io os ar java for sp 数据
============问题描述============
package com.example.sysaction; import android.net.Uri; import android.os.Build; import android.os.Bundle; import android.provider.ContactsContract; import android.annotation.SuppressLint; import android.annotation.TargetApi; import android.app.Activity; import android.content.CursorLoader; import android.content.Intent; import android.database.Cursor; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; public class SysAction extends Activity { final int PICK_CONTACT = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button bn = (Button)findViewById(R.id.bn); bn.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(); intent.setAction(Intent.ACTION_GET_CONTENT); intent.setType("vnd.android.cursor.item/phone"); startActivityForResult(intent,PICK_CONTACT); } }); } @TargetApi(Build.VERSION_CODES.HONEYCOMB) @SuppressLint("NewApi") public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); switch(requestCode) { case(PICK_CONTACT): if(resultCode == Activity.RESULT_OK) { Uri contactData = data.getData(); CursorLoader cursorLoader = new CursorLoader(this, contactData, 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 = "此联系人暂未输入电话号码"; Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +"="+contactId, null, null); if(phones.moveToFirst()) { phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); } phones.close(); EditText show = (EditText)findViewById(R.id.show); show.setText(name); EditText phone = (EditText)findViewById(R.id.phone); phone.setText(phoneNumber); } cursor.close(); } break; } } }
============解决方案1============
============解决方案2============
intent.setType("vnd.android.cursor.item/phone");
intent.setType("vnd.android.cursor.item/phone_v2");
public class SysAction extends Activity { final int PICK_CONTACT = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_sys_action); Button bn = (Button)findViewById(R.id.bn); bn.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(); intent.setAction(Intent.ACTION_GET_CONTENT); intent.setType("vnd.android.cursor.item/phone_v2"); startActivityForResult(intent,PICK_CONTACT); } }); } @TargetApi(Build.VERSION_CODES.HONEYCOMB) @SuppressLint("NewApi") public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); switch(requestCode) { case(PICK_CONTACT): if(resultCode == Activity.RESULT_OK) { Uri contactData = data.getData(); CursorLoader cursorLoader = new CursorLoader(this, contactData, null, null, null, null); Cursor cursor =cursorLoader.loadInBackground(); if (cursor.moveToFirst()) { String name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); String phoneNumber = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); EditText show = (EditText)findViewById(R.id.show); show.setText(name); EditText phone = (EditText)findViewById(R.id.phone); phone.setText(phoneNumber); } cursor.close(); } break; } } }
============解决方案3============
标签:des android io os ar java for sp 数据
原文地址:http://www.cnblogs.com/lmyangbk/p/4038767.html