public void onClick(View v) {
EditText fld=(EditText)findViewById(R.id.name);
String name=fld.getText().toString();
fld=(EditText)findViewById(R.id.phone);
String phone=fld.getText().toString();
Intent i=new Intent(Intent.ACTION_INSERT_OR_EDIT);
i.setType(Contacts.CONTENT_ITEM_TYPE);
i.putExtra(Insert.NAME, name);
i.putExtra(Insert.PHONE, phone);
startActivity(i);
}
加入包名:
import android.provider.ContactsContract.Contacts;
import android.provider.ContactsContract.Intents.Insert;
2.判断不同的版本
static {
int sdk=new Integer(Build.VERSION.SDK).intValue();
if (sdk>=5) {
try {
Class clazz=Class.forName("android.provider.ContactsContract$Contacts");
CONTENT_URI=(Uri)clazz.getField("CONTENT_URI").get(clazz);
// Log.i("clazz",""+clazz);
// Log.i("clazz.getField",""+clazz.getField("CONTENT_URI"));
// Log.i("CONTENT_URI",""+CONTENT_URI);
}
catch (Throwable t) {
Log.e("PickDemo", "Exception when determining CONTENT_URI", t);
}
}
else {
CONTENT_URI=Contacts.People.CONTENT_URI;
}
}
下面是发起一个供选择的联系人
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
if (CONTENT_URI==null) {
Toast
.makeText(this, "We are experiencing technical difficulties...",Toast.LENGTH_LONG)
.show();
finish();
return;
}
setContentView(R.layout.main);
Button btn=(Button)findViewById(R.id.pick);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent i=new Intent(Intent.ACTION_PICK, CONTENT_URI);
// Log.i("CONTENT_URI",""+CONTENT_URI);
startActivityForResult(i, PICK_REQUEST);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode,Intent data) {
if (requestCode==PICK_REQUEST) {
if (resultCode==RESULT_OK) {
startActivity(new Intent(Intent.ACTION_VIEW,data.getData()));
Log.i("data.getData()",""+data.getData());
}
}
}