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