当前位置: 首页 > news >正文

网站举报中心官网/百度一对一解答

网站举报中心官网,百度一对一解答,移动网站设计心得,北京做网站优化多少钱Android之IntentService的使用 http://blog.csdn.net/lmj623565791/article/details/47143563; 本文出自:【张鸿洋的博客】 一 概述 大家都清楚,在Android的开发中,凡是遇到耗时的操作尽可能的会交给Service去做,比如我们上传多张…

Android之IntentService的使用

 

http://blog.csdn.net/lmj623565791/article/details/47143563; 
本文出自:【张鸿洋的博客】

 

一 概述

 

大家都清楚,在Android的开发中,凡是遇到耗时的操作尽可能的会交给Service去做,比如我们上传多张图,上传的过程用户可能将应用置于后台,然后干别的去了,我们的Activity就很可能会被杀死,所以可以考虑将上传操作交给Service去做,如果担心Service被杀,还能通过设置startForeground(int, Notification)方法提升其优先级。

 

那么,在Service里面我们肯定不能直接进行耗时操作,一般都需要去开启子线程去做一些事情,自己去管理Service的生命周期以及子线程并非是个优雅的做法;好在Android给我们提供了一个类,叫做IntentService,我们看下注释。

 

IntentService is a base class for {@link Service}s that handle asynchronous 

requests (expressed as {@link Intent}s) on demand. Clients send requests 

through {@link android.content.Context#startService(Intent)} calls; the 

service is started as needed, handles each Intent in turn using a worker 

thread, and stops itself when it runs out of work.

意思说IntentService是一个基于Service的一个类,用来处理异步的请求。你可以通过startService(Intent)来提交请求,该Service会在需要的时候创建,当完成所有的任务以后自己关闭,且请求是在工作线程处理的。

 

这么说,我们使用了IntentService最起码有两个好处,一方面不需要自己去new Thread了;另一方面不需要考虑在什么时候关闭该Service了。

 

好了,那么接下来我们就来看一个完整的例子。

 

二 IntentService的使用

 

我们就来演示一个多个图片上传的案例,当然我们会模拟上传的耗时,毕竟我们的重心在IntentService的使用和源码解析上。

 

首先看下效果图

 

效果图

 

 

 

每当我们点击一次按钮,会将一个任务交给后台的Service去处理,后台的Service每处理完成一个请求就会反馈给Activity,然后Activity去更新UI。当所有的任务完成的时候,后台的Service会退出,不会占据任何内存。

 

Service

package com.zhy.blogcodes.intentservice;import android.app.IntentService;import android.content.Context;import android.content.Intent;import android.util.Log;public class UploadImgService extends IntentService{private static final String ACTION_UPLOAD_IMG = "com.zhy.blogcodes.intentservice.action.UPLOAD_IMAGE";public static final String EXTRA_IMG_PATH = "com.zhy.blogcodes.intentservice.extra.IMG_PATH";public static void startUploadImg(Context context, String path){Intent intent = new Intent(context, UploadImgService.class);intent.setAction(ACTION_UPLOAD_IMG);intent.putExtra(EXTRA_IMG_PATH, path);context.startService(intent);}public UploadImgService(){super("UploadImgService");}@Overrideprotected void onHandleIntent(Intent intent){if (intent != null){final String action = intent.getAction();if (ACTION_UPLOAD_IMG.equals(action)){final String path = intent.getStringExtra(EXTRA_IMG_PATH);handleUploadImg(path);}}}private void handleUploadImg(String path){try{//模拟上传耗时Thread.sleep(3000);Intent intent = new Intent(IntentServiceActivity.UPLOAD_RESULT);intent.putExtra(EXTRA_IMG_PATH, path);sendBroadcast(intent);} catch (InterruptedException e){e.printStackTrace();}}@Overridepublic void onCreate(){super.onCreate();Log.e("TAG","onCreate");}@Overridepublic void onDestroy(){super.onDestroy();Log.e("TAG","onDestroy");}}

 

代码很短,主要就是继承IntentService,然后复写onHandleIntent方法,根据传入的intent来选择具体的操作。startUploadImg是我写的一个辅助方法,省的每次都去构建Intent,startService了。

 

Activity

package com.zhy.blogcodes.intentservice;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.content.IntentFilter;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.widget.LinearLayout;import android.widget.TextView;import com.zhy.blogcodes.R;public class IntentServiceActivity extends AppCompatActivity{public static final String UPLOAD_RESULT = "com.zhy.blogcodes.intentservice.UPLOAD_RESULT";private LinearLayout mLyTaskContainer;private BroadcastReceiver uploadImgReceiver = new BroadcastReceiver(){@Overridepublic void onReceive(Context context, Intent intent){if (intent.getAction() == UPLOAD_RESULT){String path = intent.getStringExtra(UploadImgService.EXTRA_IMG_PATH);handleResult(path);}}};private void handleResult(String path){TextView tv = (TextView) mLyTaskContainer.findViewWithTag(path);tv.setText(path + " upload success ~~~ ");}@Overrideprotected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_intent_service);mLyTaskContainer = (LinearLayout) findViewById(R.id.id_ll_taskcontainer);registerReceiver();}private void registerReceiver(){IntentFilter filter = new IntentFilter();filter.addAction(UPLOAD_RESULT);registerReceiver(uploadImgReceiver, filter);}int i = 0;public void addTask(View view){//模拟路径String path = "/sdcard/imgs/" + (++i) + ".png";UploadImgService.startUploadImg(this, path);TextView tv = new TextView(this);mLyTaskContainer.addView(tv);tv.setText(path + " is uploading ...");tv.setTag(path);}@Overrideprotected void onDestroy(){super.onDestroy();unregisterReceiver(uploadImgReceiver);}}

 

 

Activity中,每当我点击一次按钮调用addTask,就回模拟创建一个任务,然后交给IntentService去处理。

 

注意,当Service的每个任务完成的时候,会发送一个广播,我们在Activity的onCreate和onDestroy里面分别注册和解注册了广播;当收到广播则更新指定的UI。

 

布局文件

<LinearLayout android:id="@+id/id_ll_taskcontainer"xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><Button android:layout_width="wrap_content" android:layout_height="wrap_content"android:onClick="addTask" android:text="add Task"/></LinearLayout>

ok,这样我们就完成了我们的效果图的需求;通过上例,大家可以看到我们可以使用IntentService非常方便的处理后台任务,屏蔽了诸多细节;而Service与Activity通信呢,我们选择了广播的方式(当然这里也可以使用LocalBroadcastManager)。

 

 

http://www.lbrq.cn/news/1607689.html

相关文章:

  • destoon 网站搬家/湖南网络推广排名
  • 公务员可以自己做网站吗/如何做营销策划方案
  • 如何做一张网站平面效果图/磁力兔子搜索引擎
  • 免费网站论坛/百度搜索引擎收录
  • 永康网站开发/网络营销和传统营销有什么区别
  • 宝应网站/宁波网站关键词排名推广
  • 简洁网站倒计时代码/精准客源app
  • 附近注册公司代理机构/成都网站seo
  • 注册一家公司最低需要多少钱/seo查询排名系统
  • 门户网站做啥/微信广告推广平台
  • 济南网络公司排名/优化排名工具
  • 向国旗敬礼 做新时代好少年网站/百度搜索关键词排名人工优化
  • 怎么知道网站被百度k了/什么是精准营销
  • 洛阳做网站的/seo软件哪个好
  • 武汉汉口做网站公司/北京百度竞价
  • 新手学做网站的教学书/如何申请域名
  • 汕尾东莞网站建设/google ads
  • 网站功能建设模块/北京疫情最新消息情况
  • 为知笔记发布WordPress/江门搜狗网站推广优化
  • o2o 电商网站 微商城 ppt/帮忙推广的平台
  • 电子商务网站推广方法/长沙网站seo优化公司
  • 网站外包公司/搜索推广广告
  • 工商局网站清算组备案怎么做/苏州seo
  • 网站维护流程图/seo网站优化推广教程
  • 湖州建设局投标网站/商务软文写作300
  • 签名设计网站/靠谱seo外包定制
  • wordpress建教育教学网站/数据分析师培训需要多少钱
  • 做知识内容的网站与app/新手怎么推广自己的店铺
  • 黄岩做网站/最近三天发生的重要新闻
  • 网站源码获取在线/郑州网站关键词优化公司哪家好
  • sqli-labs:Less-23关卡详细解析
  • 嵌入式学习-(李宏毅)机器学习(5)-day32
  • VisualStudio的一些开发经验
  • 北京-4年功能测试2年空窗-报培训班学测开-第六十六天
  • 从零开始学Express,理解服务器,路由于中间件
  • 【stm32】按键控制LED以及光敏传感器控制蜂鸣器