日期:2014-05-20 浏览次数:20807 次
package test.contentpickertester; 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.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class ContentPickerTester extends Activity { /** Called when the activity is first created. */ public static final int PICK_CONTACT = 1; @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.contentpickertester); Button button = (Button)findViewById(R.id.pick_contact_button); button.setOnClickListener(new OnClickListener(){ public void onClick(View _view){ Intent intent = new Intent(Intent.ACTION_PICK, Uri.parse("content://contacts/")); startActivityForResult(intent,PICK_CONTACT); } }); } @Override public void onActivityResult(int reqCode,int resCode,Intent data){ super.onActivityResult(resCode,resCode,data); switch (reqCode){ case(PICK_CONTACT):{ if(resCode == Activity.RESULT_OK){ Uri contactData = data.getData(); Cursor c = managedQuery(contactData,null,null,null,null); c.moveToFirst(); String name; name = c.getString(c.getColumnIndexOrThrow(People.NAME)); TextView tv; tv = (TextView)findViewById(R.id.selected.contact.textview); tv.setText(name); } break; } } } }