1 page.title= 2 page.tags=intents 3 helpoutsWidget=true 4 5 trainingnavtop=true 6 7 @jd:body 8 9 10 <div id="tb-wrapper"> 11 <div id="tb"> 12 13 <h2> </h2> 14 <ol> 15 <li><a href="#Build"> </a></li> 16 <li><a href="#Verify"> </a></li> 17 <li><a href="#StartActivity"> </a></li> 18 <li><a href="#AppChooser"> </a></li> 19 </ol> 20 21 <h2> </h2> 22 <ul> 23 <li><a href="{@docRoot}training/sharing/index.html"> </a></li> 24 </ul> 25 26 </div> 27 </div> 28 29 <p>Android "" 30 . 31 , 32 . {@link android.content.Intent} 33 . Android 34 .</p> 35 36 <p> <a href="{@docRoot}training/basics/firstapp/index.html"> 37 </a> , . 38 39 <em> </em> . " " 40 , <em> </em> .</p> 41 42 <p> 43 .</p> 44 45 46 47 <h2 id="Build"> </h2> 48 49 <p> , 50 . <em></em>, 51 <em></em>, <em></em> <em></em> . , 52 . 53 {@link android.net.Uri} 54 , .</p> 55 56 <p> {@link android.net.Uri} , {@link 57 android.content.Intent#Intent(String,Uri) Intent()} 58 .</p> 59 60 <p> {@link 61 android.net.Uri} .</p> 62 63 <pre> 64 Uri number = Uri.parse("tel:5551234"); 65 Intent callIntent = new Intent(Intent.ACTION_DIAL, number); 66 </pre> 67 68 <p> {@link android.app.Activity#startActivity 69 startActivity()} , .</p> 70 71 <p> {@link android.net.Uri} 72 .</p> 73 74 <ul> 75 <li> : 76 <pre> 77 // Map point based on address 78 Uri location = Uri.parse("geo:0,0?q=1600+Amphitheatre+Parkway,+Mountain+View,+California"); 79 // Or map point based on latitude/longitude 80 // Uri location = Uri.parse("geo:37.422219,-122.08364?z=14"); // z param is zoom level 81 Intent mapIntent = new Intent(Intent.ACTION_VIEW, location); 82 </pre> 83 </li> 84 <li> : 85 <pre> 86 Uri webpage = Uri.parse("http://www.android.com"); 87 Intent webIntent = new Intent(Intent.ACTION_VIEW, webpage); 88 </pre> 89 </li> 90 </ul> 91 92 <p> 93 "" . {@link 94 android.content.Intent#putExtra(String,String) putExtra()} .</p> 95 96 <p> 97 {@link android.net.Uri} MIME . {@link android.net.Uri} 98 , {@link android.content.Intent#setType setType()} 99 . MME 100 .</p> 101 102 <p> .</p> 103 104 <ul> 105 <li> : 106 <pre> 107 Intent emailIntent = new Intent(Intent.ACTION_SEND); 108 // The intent does not have a URI, so declare the "text/plain" MIME type 109 emailIntent.setType(HTTP.PLAIN_TEXT_TYPE); 110 emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[] {"jon (a] example.com"}); // recipients 111 emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Email subject"); 112 emailIntent.putExtra(Intent.EXTRA_TEXT, "Email message text"); 113 emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("content://path/to/email/attachment")); 114 // You can also attach multiple items by passing an ArrayList of Uris 115 </pre> 116 </li> 117 <li> : 118 <pre> 119 Intent calendarIntent = new Intent(Intent.ACTION_INSERT, Events.CONTENT_URI); 120 Calendar beginTime = Calendar.getInstance().set(2012, 0, 19, 7, 30); 121 Calendar endTime = Calendar.getInstance().set(2012, 0, 19, 10, 30); 122 calendarIntent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, beginTime.getTimeInMillis()); 123 calendarIntent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME, endTime.getTimeInMillis()); 124 calendarIntent.putExtra(Events.TITLE, "Ninja class"); 125 calendarIntent.putExtra(Events.EVENT_LOCATION, "Secret dojo"); 126 </pre> 127 <p class="note"><strong>:</strong> API 128 14 .</p> 129 </li> 130 </ul> 131 132 <p class="note"><strong>:</strong> {@link 133 android.content.Intent} . {@link android.content.Intent#ACTION_VIEW} 134 , 135 {@code image/*} MIME . "" 136 .</p> 137 138 139 140 <h2 id="Verify"> </h2> 141 142 <p>Android , 143 , 144 .</p> 145 146 <p class="caution"><strong>:</strong> 147 , .</p> 148 149 <p> {@link 150 android.content.pm.PackageManager#queryIntentActivities queryIntentActivities()} {@link android.content.Intent} 151 . {@link 152 java.util.List} , . .</p> 153 154 <pre> 155 PackageManager packageManager = {@link android.content.Context#getPackageManager()}; 156 List<ResolveInfo> activities = packageManager.queryIntentActivities(intent, 157 PackageManager.MATCH_DEFAULT_ONLY); 158 boolean isIntentSafe = activities.size() > 0; 159 </pre> 160 161 <p><code>isIntentSafe</code> <code>true</code> , 162 . <code>false</code> , .</p> 163 164 <p class="note"><strong>:</strong> 165 , 166 . , 167 (<a href="{@docRoot}distribute/tools/promote/linking.html">Google 168 Play </a> ).</p> 169 170 171 <h2 id="StartActivity"> </h2> 172 173 <div class="figure" style="width:200px;margin-top:-10px"> 174 <img src="{@docRoot}images/training/basics/intents-choice.png" alt="" /> 175 <p class="img-caption"><strong> 1.</strong> . 176 </p> 177 </div> 178 179 <p>{@link android.content.Intent} {@link 180 android.app.Activity#startActivity startActivity()} . 181 , 1 182 . 183 , .</p> 184 185 <pre> 186 startActivity(intent); 187 </pre> 188 189 <p> , 190 , .</p> 191 192 <pre> 193 // Build the intent 194 Uri location = Uri.parse("geo:0,0?q=1600+Amphitheatre+Parkway,+Mountain+View,+California"); 195 Intent mapIntent = new Intent(Intent.ACTION_VIEW, location); 196 197 // Verify it resolves 198 PackageManager packageManager = {@link android.content.Context#getPackageManager()}; 199 List<ResolveInfo> activities = packageManager.queryIntentActivities(mapIntent, 0); 200 boolean isIntentSafe = activities.size() > 0; 201 202 // Start an activity if it's safe 203 if (isIntentSafe) { 204 startActivity(mapIntent); 205 } 206 </pre> 207 208 209 210 <h2 id="AppChooser"> </h2> 211 212 <div class="figure" style="width:200px;margin-top:-10px"> 213 <img src="{@docRoot}images/training/basics/intent-chooser.png" alt="" /> 214 <p class="img-caption"><strong> 2.</strong> .</p> 215 </div> 216 217 <p>{@link android.content.Intent} {@link 218 android.app.Activity#startActivity startActivity()} 219 , ( 220 . 1 ). 221 . ( 222 ) ( ) .</p> 223 224 <p> "" 225 , 226 . , 2 227 . 228 ( 229 ).</p> 230 231 <p> {@link 232 android.content.Intent#createChooser createChooser()} {@link android.content.Intent} {@link 233 android.app.Activity#startActivity startActivity()} . .</p> 234 235 <pre> 236 Intent intent = new Intent(Intent.ACTION_SEND); 237 ... 238 239 // Always use string resources for UI text. 240 // This says something like "Share this photo with" 241 String title = getResources().getString(R.string.chooser_title); 242 // Create intent to show chooser 243 Intent chooser = Intent.createChooser(intent, title); 244 245 // Verify the intent will resolve to at least one activity 246 if (intent.resolveActivity(getPackageManager()) != null) { 247 startActivity(chooser); 248 } 249 </pre> 250 251 <p> {@link 252 android.content.Intent#createChooser createChooser()} 253 .</p> 254 255 256 257