Send work requests to the background service

The previous lesson showed you how to create a JobIntentService class. This lesson shows you how to trigger theJobIntentService to run an operation by enqueuing work with an Intent. This Intent can optionally contain data for the JobIntentService to process.

教你咋把Intent enqueue

 

Create and send a work request to a JobIntentService

To create a work request and send it to a JobIntentService, create an Intent and enqueue it to be executed by calling enqueueWork(). Optionally you can add data to the intent (in the form of intent extras) for the JobIntentService to process. For more information about creating intents, read the Building an intent section in Intents and Intent Filters

创建并发送work request到 JobIntentService

The following code snippets demonstrate this process:

看下文

Create a new Intent for the JobIntentService called RSSPullService

1.创建Intent

/* * Creates a new Intent to start the RSSPullService * JobIntentService. Passes a URI in the * Intent's "data" field. */serviceIntent = new Intent();serviceIntent.putExtra("download_url", dataUrl));

2.Call enqueueWork() 

调用 enqueueWork

// Starts the JobIntentServiceprivate static final int RSS_JOB_ID = 1000;RSSPullService.enqueueWork(getContext(), RSSPullService.class, RSS_JOB_ID, serviceIntent);

 

Notice that you can send the work request from anywhere in an Activity or Fragment. For example, if you need to get user input first, you can send the request from a callback that responds to a button click or similar gesture.

这个request在Activity或者Fragment 的任何地方都能调用

 

Once you call enqueueWork(), the JobIntentService does the work defined in its onHandleWork() method, and then stops itself.

调用 enqueueWork,则JobIntentService 在onHandleWork里面 does the work,干完活自己stop

 

The next step is to report the results of the work request back to the originating Activity or Fragment. The next lesson shows you how to do this with a BroadcastReceiver.

下文讲如何report the result(通过广播)

 

 

更多相关文章

  1. 探索FragmentTransaction#commit()抛出IllegalStateException
  2. Android学习笔记-Android初级 (二)
  3. Android中模拟HOME键功能
  4. android activity与view的联系--window
  5. Android(安卓)Binder分析五:Java service的获取和调用
  6. 使用Android(安卓)Studio进行单元测试
  7. 浅析Android手机卫士关闭自动更新
  8. Android(安卓)调用相册 拍照 实现系统控件缩放 切割图片 .
  9. Android调用Camera,获取预览帧中的图像

随机推荐

  1. Android引入签名文件
  2. Android(安卓)8.1 来电显示SIM卡
  3. Android:获取APK签名信息
  4. Android studio 3.1.4 无法获取pom文件
  5. Android NDK开发之Android.mk文件
  6. Android(安卓)progressBar代码设置进度条
  7. Android 单独抽取 WebRtc-AGC(音频增益) 模
  8. 【转】Android Makefile中是 如何识别 TA
  9. 安卓开发入门学习笔记
  10. Android知识体系总结之Android部分View绘