Android学习总结(三)——IntentService的用法
一.IntentService的基本概念
IntentService是继承于Service并处理异步请求的一个类,在IntentService内有一个工作线程来处理耗时操作,启动IntentService的方式和启动传统Service一样,同时,当任务执行完后,IntentService会自动停止,而不需要我们去手动控制。另外,可以启动IntentService多次,而每一个耗时操作会以工作队列的方式在IntentService的onHandleIntent回调方法中执行,并且,每次只会执行一个工作线程,执行完第一个再执行第二个,以此类推。所有请求都在一个单线程中,不会阻塞应用程序的主线程(UI Thread),同一时间只处理一个请求。那么,用IntentService有什么好处呢?首先,我们省去了在Service中手动开线程的麻烦,第二,当操作完成时,我们不用手动停止Service,第三,it's so easy to use!
二.开启代码之旅
OK!接下看看如何去使用,新建工程,新建一个继承IntentService的类,代码如下:
package com.nyl.intentservicesummarize; import android.app.IntentService;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log; /**
* Created by Administrator on 2017/3/4 0004.
*/ public class ServiceTest extends IntentService{ private static final String TAG = ServiceTest.class.getSimpleName(); //必须实现父类的构造方法
public ServiceTest() {
super("aa");
} /**
* 必须重写的核心方法
* @param intent
*/
@Override
protected void onHandleIntent(Intent intent) {
//Intent是从Activity发过来的,携带识别参数,根据参数不同执行不同的任务
String action = intent.getExtras().getString("param");
if (action.equals("s1")) {
Log.i(TAG,"启动第一个service");
}else if (action.equals("s2")){
Log.i(TAG,"启动第二个service");
}else if (action.equals("s3")){
Log.i(TAG,"启动第三个service"); try {
//让服务休眠2秒
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
} } } /**
* 重写其他方法,用于查看方法的调用顺序
* @param intent
* @return
*/
@Override
public IBinder onBind(Intent intent) {
Log.i(TAG,"onBind");
return super.onBind(intent);
} @Override
public void onCreate() {
super.onCreate();
} @Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(TAG,"onStartCommand");
return super.onStartCommand(intent, flags, startId);
} @Override
public void setIntentRedelivery(boolean enabled) {
super.setIntentRedelivery(enabled);
Log.i(TAG,"setIntentRedelivery");
} @Override
public void onDestroy() {
Log.i(TAG,"onDestroy");
super.onDestroy();
}
}
AndroidManifest.xml注册下Service

在MainActivity启动三次服务,代码如下:
package com.nyl.intentservicesummarize; import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button; public class MainActivity extends Activity implements View.OnClickListener { private Button btnStartIntentService; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); btnStartIntentService = (Button) findViewById(R.id.btnStartIntentService);
btnStartIntentService.setOnClickListener(this);
} @Override
public void onClick(View view) {
if (view.getId() == R.id.btnStartIntentService){ /**
* 有些时候我们使用Service的时需要采用隐私启动的方式,
* 但是Android 5.0一出来后,其中有个特性就是Service Intent must be explitict,
* 也就是说从Lollipop开始,service服务必须采用显示方式启动。
* 解决方法: 设置Intent的Action和packageName
*/
Intent it1 = new Intent();
it1.setAction("com.test.intentservice");
it1.setPackage(getPackageName());
Bundle b1 = new Bundle();
b1.putString("param", "s1");
it1.putExtras(b1); Intent it2 = new Intent("com.test.intentservice");
it2.setPackage(getPackageName());
Bundle b2 = new Bundle();
b2.putString("param", "s2");
it2.putExtras(b2); Intent it3 = new Intent("com.test.intentservice");
it3.setPackage(getPackageName());
Bundle b3 = new Bundle();
b3.putString("param", "s3");
it3.putExtras(b3); //接着启动多次IntentService,每次启动,都会新建一个工作线程
//但始终只有一个IntentService实例
startService(it1);
startService(it2);
startService(it3);
}
} }
代码之路已经完成了,我们来看看打印的结果吧,如下图:

关于IntentService的内容总结暂时就这么多,文章如果有写得不对的地方,欢迎广大园友指正!
Android学习总结(三)——IntentService的用法的更多相关文章
- 三、Android学习第三天——Activity的布局初步介绍(转)
(转自:http://wenku.baidu.com/view/af39b3164431b90d6c85c72f.html) 三.Android学习第三天——Activity的布局初步介绍 今天总结下 ...
- Android学习第三天-打包常用命令
在前面<Android学习第一天-adb常用命令>和 <Android学习第二天-android常用命令>两篇博文中,我们重点讲解了adb和android的常用命令,下面我们讲 ...
- 【Android】完善Android学习(三:API 3.0)
备注:之前Android入门学习的书籍使用的是杨丰盛的<Android应用开发揭秘>,这本书是基于Android 2.2API的,目前Android已经到4.4了,更新了很多的API,也增 ...
- Android学习(三)
学号 20189214 <Android程序开发>第八周学习总结 教材学习内容总结 GridView GridView和ListView一样是AbsListView的子类; 都需要一个Ad ...
- android学习笔记三
GUI==>Graphics User Interface,图形用户界面. android UI 建立在View.ViewGroup基础上,采用组合器设计模式设计View和ViewGoup. V ...
- Android学习--持久化(三) SQLite & LitePal
SQLite & LitePal 自己做为一个iOS开发,看到安卓这一块的时候,那中浓烈的熟悉味道更加强烈,SQLite这种轻量级的关系型数据库的使用在移动端相差不多,iOS有FMDB,And ...
- Android学习第三天-签名常用命令
由于怕篇幅过长,所以把这个打包常用命令分开成两篇博文来进行讲解,下面我们直接进入主题吧. 8.keytool 这是我们JDK自带的密钥和证书管理工具 命令: -certreq 生成证书请求 -chan ...
- Android学习笔记三:用Intent串联activity
转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/7513399.html 一:Intent Intent可以理解为 意图. 我们可以通过创建intent实例来定义 ...
- Android学习(三) 自动完成的使用
1.AutoCompleteTextView 自动完成功能,在文本框中输入字符,会出现匹配的自动提示.类似百度搜索. XML代码 <?xml version="1.0" en ...
- Android学习笔记(三)
ContentProvider简单介绍 ContentProvider是不同应用程序之间进行数据交换的标准API,当一个应用程序须要把自己的数据暴露给其它程序使用时.该应用程序便可通过提供Conten ...
随机推荐
- 使用memcpy 复制unsigned int 型的数据
转载请注明出处:http://blog.csdn.net/qq_26093511/article/details/53214692 函数原型: void *memcpy(void *dest, con ...
- CS231n 2016 通关 第四章-反向传播与神经网络(第一部分)
在上次的分享中,介绍了模型建立与使用梯度下降法优化参数.梯度校验,以及一些超参数的经验. 本节课的主要内容: 1==链式法则 2==深度学习框架中链式法则 3==全连接神经网络 =========== ...
- mysql order by是怎么工作的?
假设我们要查询一个市民表中城市=杭州的所有人的名字,并且按照名字排序 CREATE TABLE `t` ( `id` ) NOT NULL, `city` ) NOT NULL, `name` ) N ...
- 如何在html中引入jsx文件
不使用webpack工具做react项目 1.引入react相关js文件 <script src="https://cdn.staticfile.org/react/16.4.0/um ...
- 剖析JSONP原理的小例子
1. 服务器端代码(Node.js) // 导入 http 内置模块 const http = require('http'); // 这个核心模块,能够帮我们解析 URL地址,从而拿到 pathna ...
- Lightoj1122 【数位DP】
题意: 给你m个数,让你在里面挑n个组合,保证位数相差不超过2,求能够组合多少种情况: 思路: dp[i][j]代表第i个结尾为j的方案数. #include<bits/stdc++.h> ...
- 【Linux】Devops的一些运维工具
一.Devops简介 从手工编译.上传服务器文件.执行命令.启动停止服务器.发现BUG再重复一遍流程,软件开发的重复劳动越来越多,在Devops概念之前,全部要靠人工手动完成,也看到了很多运维人员半夜 ...
- C#:索引
1. 什么是索引 索引是一组get和set访问器,类似于属性的访问器. 2. 索引和属性 和属性一样,索引不用分配内存来存储 索引和属性都主要被用来访问其他数据成员,这些成员和它们关联,它们为这些成员 ...
- echarts相关属性设置(2)--折线图和柱状图的结合使用
type:bar和line的组合 option = { { tooltip: { trigger: 'axis', axisPointer: { // type: 'shadow' }, // lab ...
- E. Cyclic Components (DFS)(Codeforces Round #479 (Div. 3))
#include <bits/stdc++.h> using namespace std; *1e5+; vector<int>p[maxn]; vector<int&g ...