博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
第19天Service-IntentService
阅读量:3788 次
发布时间:2019-05-22

本文共 3606 字,大约阅读时间需要 12 分钟。

第19天Service-IntentService

IntentService

一.IntentService介绍

IntentService,可以看做是Service和HandlerThread的结合体,在完成了使命之后会自动停止,适合需要在工作线程处理UI无关任务的场景。

  • IntentService 是继承自 Service 并处理异步请求的一个类,在 IntentService
    内有一个工作线程来处理耗时操作。
  • 当任务执行完后,IntentService 会自动停止,不需要我们去手动结束。
  • 如果启动 IntentService 多次,那么每一个耗时操作会以工作队列的方式在 IntentService 的
    onHandleIntent 回调方法中执行,依次去执行,使用串行的方式,执行完自动结束。

二.IntentService的优点:不用开启线程

三.IntentService的缺点:使用广播向activity传值

四.案例:使用IntentService网络请求json串,将json串使用广播发送给activity界面

思路:创建服务,注册服务 ,

(1)创建服务:MyIntentService.java

public class MyIntentService extends IntentService {
//TODO 注意:必须提供无参数构造,不然清单文件中注册报错 public MyIntentService(){
super("MyIntentService"); } public MyIntentService(String name) {
super(name); } //TODO 将所有耗时操作都在该方法中完成,相当于开启线程 @Override protected void onHandleIntent(@Nullable Intent intent) {
StringBuffer sb=new StringBuffer(); HttpURLConnection connection=null; InputStream inputStream=null; try {
URL url=new URL("http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=20&page=1"); connection = (HttpURLConnection) url.openConnection(); connection.setReadTimeout(5*1000); connection.setConnectTimeout(5*1000); if(connection.getResponseCode()==200){
inputStream = connection.getInputStream(); byte[] b=new byte[1024]; int len=0; while((len=inputStream.read(b))!=-1){
sb.append(new String(b,0,len)); } //TODO 向Activity或Fragment发送json串 Intent intent1=new Intent(); intent1.setAction("com.bawei.intentservice"); Bundle bundle = new Bundle(); bundle.putString("json",sb.toString()); intent1.putExtras(bundle); sendBroadcast(intent1); } } catch (IOException e) {
e.printStackTrace(); }finally{
if(inputStream!=null){
try {
inputStream.close(); } catch (IOException e) {
e.printStackTrace(); } } if(connection!=null){
connection.disconnect(); } } }}

(2)清单文件注册服务

(3)Activity代码

public class MainActivity extends AppCompatActivity {
private MyReceiver myReceiver; private Intent intent; @Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //注册广播 IntentFilter intentFilter=new IntentFilter(); intentFilter.addAction("com.bawei.intentservice"); myReceiver=new MyReceiver(); registerReceiver(myReceiver,intentFilter); //开启服务 intent=new Intent(this,MyIntentService.class); startService(intent); } //解除广播+关闭服务 @Override protected void onDestroy() {
super.onDestroy(); unregisterReceiver(myReceiver); stopService(intent); } class MyReceiver extends BroadcastReceiver{
@Override public void onReceive(Context context, Intent intent) {
String action=intent.getAction(); if("com.bawei.intentservice".equals(action)){
Bundle bundle = intent.getExtras(); String json = bundle.getString("json", ""); //接续json串 展现在ListView 中 Toast.makeText(context, ""+json, Toast.LENGTH_SHORT).show(); } } }}

转载地址:http://cwktn.baihongyu.com/

你可能感兴趣的文章
PyCharm使用技巧及常用快捷键
查看>>
ubuntu内存爆满卡住,一顿操作任务栏菜单栏消失再解决办法记录
查看>>
ubuntu下pycharm无法输入中文解决办法(记录)
查看>>
torch.cuda.is_available()返回False的解决办法
查看>>
BITVehicle_Dataset数据集转换
查看>>
将视频转存成图片小代码
查看>>
ImportError: cannot import name ‘Line 解决方法
查看>>
Ubuntu 创建/删除虚拟环境
查看>>
deepsort算法中绘制轨迹部分的代码【记录】
查看>>
C++程序设计作业--坦克大战[分享]
查看>>
Uuntu20.04出现“qt.qpa.plugin: Could not load the Qt platform plugin “xcb“ in...已放弃 (核心已转储)”问题解决记录
查看>>
Linux系统常用的基本操作记录
查看>>
ZeroDivisionError: integer division or modulo by zero解决记录
查看>>
使用软链接放置数据集
查看>>
wx-charts折线统计图的实现(以每日体重展示为例)
查看>>
Windows消息:如何自定义窗口消息与线程消息
查看>>
Windows消息:怎样使用RegisterWindowMessage注册消息
查看>>
MultiSlider组件
查看>>
TransparentBitmap函数设置透明位图的原理分析
查看>>
透明位图的显示(TransparentBlt函数)
查看>>