1 page.title= 2 page.tags= 3 helpoutsWidget=true 4 5 trainingnavtop=true 6 7 @jd:body 8 9 <div id="tb-wrapper"> 10 <div id="tb"> 11 12 <h2></h2> 13 <ol> 14 <li><a href="#StartActivity"></a></li> 15 <li><a href="#ReceiveResult"></a></li> 16 </ol> 17 18 <h2></h2> 19 <ul> 20 <li><a href="{@docRoot}training/sharing/index.html"></a></li> 21 <li><a href="{@docRoot}training/secure-file-sharing/index.html"></a> 22 </ul> 23 24 </div> 25 </div> 26 27 <p> 28 {@link android.app.Activity#startActivityForResult 29 startActivityForResult()} ( {@link android.app.Activity#startActivity 30 startActivity()})</p> 31 32 <p> () 33 34 </p> 35 36 <p> {@link android.content.Intent} 37 {@link android.app.Activity#onActivityResult onActivityResult()} 38 </p> 39 40 <p class="note"><strong></strong> {@link android.app.Activity#startActivityForResult startActivityForResult()} 41 42 43 </p> 44 45 46 <h2 id="StartActivity"></h2> 47 48 <p> {@link android.content.Intent} {@link 49 android.app.Activity#startActivityForResult startActivityForResult()} 50 </p> 51 52 <p> {@link android.content.Intent} 53 54 </p> 55 56 <p> ()</p> 57 58 <pre> 59 static final int PICK_CONTACT_REQUEST = 1; // The request code 60 ... 61 private void pickContact() { 62 Intent pickContactIntent = new Intent(Intent.ACTION_PICK, Uri.parse("content://contacts")); 63 pickContactIntent.setType(Phone.CONTENT_TYPE); // Show user only contacts w/ phone numbers 64 startActivityForResult(pickContactIntent, PICK_CONTACT_REQUEST); 65 } 66 </pre> 67 68 69 <h2 id="ReceiveResult"></h2> 70 71 <p> {@link android.app.Activity#onActivityResult onActivityResult()} 72 73 </p> 74 75 <ul> 76 <li> {@link 77 android.app.Activity#startActivityForResult startActivityForResult()} </li> 78 <li> {@link 79 android.app.Activity#RESULT_OK} () {@link 80 android.app.Activity#RESULT_CANCELED} () 81 </li> 82 <li> {@link android.content.Intent}</li> 83 </ul> 84 85 <p></p> 86 87 <pre> 88 @Override 89 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 90 // Check which request we're responding to 91 if (requestCode == PICK_CONTACT_REQUEST) { 92 // Make sure the request was successful 93 if (resultCode == RESULT_OK) { 94 // The user picked a contact. 95 // The Intent's data Uri identifies which contact was selected. 96 97 // Do something with the contact here (bigger example below) 98 } 99 } 100 } 101 </pre> 102 103 <p> Android {@link android.content.Intent} {@link android.net.Uri} () 104 105 </p> 106 107 <p> {@link android.content.Intent} 108 109 Android API 110 () URI () {@code "data"} {@link android.graphics.Bitmap} (<a href="{@docRoot}training/camera/index.html"></a>) 111 112 113 </p> 114 115 116 <h4></h4> 117 118 <p> 119 <a href="{@docRoot}guide/topics/providers/content-providers.html"></a> 120 121 122 </p> 123 124 <pre> 125 @Override 126 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 127 // Check which request it is that we're responding to 128 if (requestCode == PICK_CONTACT_REQUEST) { 129 // Make sure the request was successful 130 if (resultCode == RESULT_OK) { 131 // Get the URI that points to the selected contact 132 Uri contactUri = data.getData(); 133 // We only need the NUMBER column, because there will be only one row in the result 134 String[] projection = {Phone.NUMBER}; 135 136 // Perform the query on the contact to get the NUMBER column 137 // We don't need a selection or sort order (there's only one result for the given URI) 138 // CAUTION: The query() method should be called from a separate thread to avoid blocking 139 // your app's UI thread. (For simplicity of the sample, this code doesn't do that.) 140 // Consider using {@link android.content.CursorLoader} to perform the query. 141 Cursor cursor = getContentResolver() 142 .query(contactUri, projection, null, null, null); 143 cursor.moveToFirst(); 144 145 // Retrieve the phone number from the NUMBER column 146 int column = cursor.getColumnIndex(Phone.NUMBER); 147 String number = cursor.getString(column); 148 149 // Do something with the phone number... 150 } 151 } 152 } 153 </pre> 154 155 <p class="note"><strong></strong> Android 2.3 (API 9) {@link android.provider.ContactsContract.Contacts Contacts Provider} () {@link 156 android.Manifest.permission#READ_CONTACTS} (<a href="{@docRoot}guide/topics/security/security.html"></a>) 157 158 Android 2.3 / 159 160 {@link 161 android.Manifest.permission#READ_CONTACTS} {@link android.net.Uri} 162 163 </p> 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179