startActivityForResult and onActivityResult
4122 ワード
startActivityForResult and onActivityResult
startActivityForResult Activity ,onActivityResult
1 public class MyActivity extends Activity {
2 ...
3
4 static final int PICK_CONTACT_REQUEST = 0;
5
6 protected boolean onKeyDown(int keyCode, KeyEvent event) {
7 if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
8 // When the user center presses, let them pick a contact.
9 startActivityForResult(
10 new Intent(Intent.ACTION_PICK,
11 new Uri("content://contacts")),
12 PICK_CONTACT_REQUEST);
13 return true;
14 }
15 return false;
16 }
17
18 protected void onActivityResult(int requestCode, int resultCode,
19 Intent data) {
20 if (requestCode == PICK_CONTACT_REQUEST) {
21 if (resultCode == RESULT_OK) {
22 // A contact was picked. Here we will just display it
23 // to the user.
24 startActivity(new Intent(Intent.ACTION_VIEW, data));
25 }
26 }
27 }