Android开发入门:代码示例解析
public class MyActivity extends Activity {
static final int PICK_CONTACT_REQUEST = 0;
protected boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
// 当用户按下中心键时,允许他们选择一个联系人
startActivityForResult(
new Intent(Intent.ACTION_PICK,
new Uri("content://contacts")),
PICK_CONTACT_REQUEST);
return true;
}
return false;
}
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
if (requestCode == PICK_CONTACT_REQUEST) {
if (resultCode == RESULT_OK) {
// 已选择联系人,此处仅将其显示给用户
startActivity(new Intent(Intent.ACTION_VIEW, data));
}
}
}
}