1 page.title= 2 page.metaDescription= 3 page.keywords="android N", "implicit broadcasts", "job scheduler" 4 page.image=images/cards/card-nyc_2x.jpg 5 6 @jd:body 7 8 <div id="qv-wrapper"> 9 <div id="qv"> 10 <h2> 11 12 </h2> 13 14 <ol> 15 <li> 16 <a href="#connectivity-action">CONNECTIVITY_ACTION </a> 17 </li> 18 19 <li> 20 <a href="#sched-jobs"></a> 21 22 </li> 23 24 <li> 25 <a href="#monitor-conn"></a> 26 27 </li> 28 29 <li> 30 <a href="#media-broadcasts">NEW_PICTURE NEW_VIDEO </a> 31 32 </li> 33 34 <li> 35 <a href="#new-jobinfo"> JobInfo </a> 36 </li> 37 38 <li> 39 <a href="#new-jobparam"> JobParameter </a> 40 </li> 41 42 <li> 43 <a href="#further-optimization"></a> 44 </li> 45 </ol> 46 </div> 47 </div> 48 49 <p> 50 51 52 53 54 </p> 55 56 <p> 57 N Developer Preview 58 59 </p> 60 61 <ul> 62 <li> Preview {@link 63 android.net.ConnectivityManager#CONNECTIVITY_ACTION} 64 {@link 65 android.content.Context#registerReceiver Context.registerReceiver()} {@link android.content.BroadcastReceiver} {@code CONNECTIVITY_CHANGE} 66 67 68 </li> 69 70 <li> {@link 71 android.hardware.Camera#ACTION_NEW_PICTURE} {@link 72 android.hardware.Camera#ACTION_NEW_VIDEO} Preview 73 74 </li> 75 </ul> 76 77 <p> 78 Android 79 {@link android.app.job.JobScheduler} <a href="https://developers.google.com/android/reference/com/google/android/gms/gcm/GcmNetworkManager"> 80 {@code GcmNetworkManager}</a> () 81 82 83 {@link android.app.job.JobScheduler} {@link android.app.job.JobInfo} {@link android.app.job.JobScheduler} 84 85 86 {@link android.app.job.JobService} 87 88 </p> 89 90 <p> 91 ( 92 {@link android.app.job.JobScheduler}) 93 94 </p> 95 96 <h2 id="connectivity-action"> 97 CONNECTIVITY_ACTION 98 </h2> 99 100 <p> 101 N Developer Preview {@link 102 android.net.ConnectivityManager#CONNECTIVITY_ACTION} 103 104 105 106 Android 107 108 109 </p> 110 111 <p class="note"> 112 <strong></strong> 113 {@link android.content.Context#registerReceiver Context.registerReceiver()} 114 {@link android.content.BroadcastReceiver} 115 </p> 116 117 <h3 id="sched-jobs"> 118 119 </h3> 120 121 <p> 122 {@link android.app.job.JobInfo.Builder JobInfo.Builder} {@link android.app.job.JobInfo} {@link 123 android.app.job.JobInfo.Builder#setRequiredNetworkType 124 setRequiredNetworkType()} {@link android.app.job.JobInfo 125 JobInfo.NETWORK_TYPE_UNMETERED} 126 127 128 129 </p> 130 131 <pre> 132 public static final int MY_BACKGROUND_JOB = 0; 133 ... 134 public static void scheduleJob(Context context) { 135 JobScheduler js = 136 (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE); 137 JobInfo job = new JobInfo.Builder( 138 MY_BACKGROUND_JOB, 139 new ComponentName(context, MyJobService.class)) 140 .setRequiredNetworkType(JobInfo.NETWORK_TYPE_UNMETERED) 141 .setRequiresCharging(true) 142 .build(); 143 js.schedule(job); 144 } 145 </pre> 146 147 <p> 148 {@code JobService.class} {@link android.app.job.JobService#onStartJob onStartJob()} 149 150 {@link 151 android.app.job.JobScheduler} <a href="{@docRoot}samples/JobScheduler/index.html">JobScheduler </a> 152 </p> 153 154 <p> 155 GMSCore Android 5.0 (API 21) <a href="https://developers.google.com/android/reference/com/google/android/gms/gcm/GcmNetworkManager"> 156 {@code GcmNetworkManager}</a> {@code Task.NETWORK_STATE_UNMETERED} 157 158 </p> 159 160 <h3 id="monitor-conn"> 161 162 </h3> 163 164 <p> 165 {@link 166 android.content.BroadcastReceiver} {@code 167 CONNECTIVITY_CHANGE}{@link 168 android.net.ConnectivityManager} API 169 170 </p> 171 172 <p> 173 {@link android.net.NetworkRequest} {@link android.net.NetworkCapabilities} 174 {@link 175 android.net.NetworkRequest.Builder NetworkRequest.Builder} {@link android.net.NetworkRequest} {@link 176 android.net.ConnectivityManager#registerNetworkCallback(android.net.NetworkRequest, 177 android.net.ConnectivityManager.NetworkCallback) registerNetworkCallback()} 178 {@link android.net.NetworkRequest} 179 {@link 180 android.net.ConnectivityManager.NetworkCallback} 181 {@link android.net.ConnectivityManager.NetworkCallback#onAvailable 182 onAvailable()} 183 184 </p> 185 186 <p> 187 188 {@link android.net.ConnectivityManager#unregisterNetworkCallback 189 unregisterNetworkCallback()} 190 </p> 191 192 <h2 id="media-broadcasts"> 193 NEW_PICTURE NEW_VIDEO 194 </h2> 195 196 <p> 197 N Developer Preview {@link 198 android.hardware.Camera#ACTION_NEW_PICTURE} {@link 199 android.hardware.Camera#ACTION_NEW_VIDEO} 200 201 N Developer Preview {@link android.app.job.JobInfo} {@link 202 android.app.job.JobParameters} 203 204 </p> 205 206 <h3 id="new-jobinfo"> 207 JobInfo 208 </h3> 209 210 <p> 211 URI N Developer Preview {@link android.app.job.JobInfo} API 212 213 </p> 214 215 <dl> 216 <dt> 217 {@code JobInfo.TriggerContentUri()} 218 </dt> 219 220 <dd> 221 URI 222 </dd> 223 224 <dt> 225 {@code JobInfo.Builder.addTriggerContentUri()} 226 </dt> 227 228 <dd> 229 {@code TriggerContentUri} {@link 230 android.app.job.JobInfo}{@link android.database.ContentObserver} URI 231 {@code 232 TriggerContentUri} URI 233 234 </dd> 235 236 <dd> 237 {@code TriggerContentUri.FLAG_NOTIFY_FOR_DESCENDANTS} URI 238 {@link 239 android.content.ContentResolver#registerContentObserver 240 registerContentObserver()} {@code notifyForDescendants} 241 242 </dd> 243 </dl> 244 245 <p class="note"> 246 <strong></strong>{@code TriggerContentUri()} {@link android.app.job.JobInfo.Builder#setPeriodic 247 setPeriodic()} {@link android.app.job.JobInfo.Builder#setPersisted 248 setPersisted()} 249 {@link 250 android.app.job.JobService} 251 {@link android.app.job.JobInfo} 252 </p> 253 254 <p> 255 URI {@code MEDIA_URI} 256 257 </p> 258 259 <pre> 260 public static final int MY_BACKGROUND_JOB = 0; 261 ... 262 public static void scheduleJob(Context context) { 263 JobScheduler js = 264 (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE); 265 JobInfo.Builder builder = new JobInfo.Builder( 266 MY_BACKGROUND_JOB, 267 new ComponentName(context, MediaContentJob.class)); 268 builder.addTriggerContentUri( 269 new JobInfo.TriggerContentUri(MEDIA_URI, 270 JobInfo.TriggerContentUri.FLAG_NOTIFY_FOR_DESCENDANTS)); 271 js.schedule(builder.build()); 272 } 273 </pre> 274 <p> 275 URI {@link android.app.job.JobParameters} {@code MediaContentJob.class} {@link android.app.job.JobService#onStartJob onStartJob()} 276 277 278 279 </p> 280 281 <h3 id="new-jobparam"> 282 JobParameter 283 </h3> 284 285 <p> 286 N Developer Preview {@link android.app.job.JobParameters} URI 287 288 289 </p> 290 291 <dl> 292 <dt> 293 {@code Uri[] getTriggeredContentUris()} 294 </dt> 295 296 <dd> 297 URI URI () URI 50 {@code 298 null} 299 300 301 </dd> 302 303 <dt> 304 {@code String[] getTriggeredContentAuthorities()} 305 </dt> 306 307 <dd> 308 309 {@code null} {@code getTriggeredContentUris()} URI 310 311 </dd> 312 </dl> 313 314 <p> 315 {@link 316 android.app.job.JobService#onStartJob JobService.onStartJob()} URI 317 318 </p> 319 320 <pre> 321 @Override 322 public boolean onStartJob(JobParameters params) { 323 StringBuilder sb = new StringBuilder(); 324 sb.append("Media content has changed:\n"); 325 if (params.getTriggeredContentAuthorities() != null) { 326 sb.append("Authorities: "); 327 boolean first = true; 328 for (String auth : 329 params.getTriggeredContentAuthorities()) { 330 if (first) { 331 first = false; 332 } else { 333 sb.append(", "); 334 } 335 sb.append(auth); 336 } 337 if (params.getTriggeredContentUris() != null) { 338 for (Uri uri : params.getTriggeredContentUris()) { 339 sb.append("\n"); 340 sb.append(uri); 341 } 342 } 343 } else { 344 sb.append("(No content)"); 345 } 346 Log.i(TAG, sb.toString()); 347 return true; 348 } 349 </pre> 350 351 <h2 id="further-optimization"> 352 353 </h2> 354 355 <p> 356 357 358 359 N Developer Preview 360 361 362 363 </p> 364 365 <p> 366 N Developer Preview <a href="{@docRoot}tools/help/adb.html">Android Debug Bridge (ADB)</a> 367 368 </p> 369 370 <ul> 371 <li> 372 373 </li> 374 375 <li style="list-style: none; display: inline"> 376 <pre class="no-pretty-print"> 377 {@code $ adb shell cmd appops set RUN_IN_BACKGROUND ignore} 378 </pre> 379 </li> 380 381 <li> 382 383 </li> 384 385 <li style="list-style: none; display: inline"> 386 <pre class="no-pretty-print"> 387 {@code $ adb shell cmd appops set RUN_IN_BACKGROUND allow} 388 </pre> 389 </li> 390 </ul>