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 ...
随机推荐
- jq之鼠标事件
以防自己忘记,最重要的是hover效果的 鼠标事件是在用户移动鼠标光标或者使用任意鼠标键点击时触发的. (1):click事件:click事件于用户在元素敲击鼠标左键,并在相同元素上松开左键时触发 ...
- POJ-3629
Card Stacking Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3927 Accepted: 1541 Des ...
- C++ 预处理指令 #pragma
http://www.cnblogs.com/qinfengxiaoyue/archive/2012/06/05/2535524.html
- ESXI中 Linux虚拟机不重启扩展磁盘
1.首先对虚拟机进行编辑设置 硬盘大小进行修改到80G: 2.在Linux系统中查看磁盘大小 此时并没有什么变化: 3. 上面没有变化的原因,是因为需要重新扫描存储设备的scsi总线: 找到scsi磁 ...
- 技术胖Flutter第四季-24Flutter的打包
视频地址: https://www.bilibili.com/video/av35800108/?p=25 文章地址: https://jspang.com/post/flutter4.html#to ...
- Flutter实战视频-移动电商-36.FlutterToast插件使用
36.FlutterToast插件使用 https://github.com/PonnamKarthik/FlutterToast fluttertoast: ^ category_page.dart ...
- CodeForces 586D【BFS】
题意: s是这个人开始位置:连续相同大写字母是 Each of the k trains,相应的火车具有相应的字母: '.' 代表空: 有个人在最左列,上面有连续字母代表的火车,火车从左边出去的话,会 ...
- 51nod1416(dfs)
题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1416 题意:中文题诶- 思路:dfs 搜索同一颜色的点.. 只 ...
- 原生js回到顶部
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- plt
设定X,Y轴的长度以及刻度的方法. import numpy as np import matplotlib.pyplot as plt data = np.arange(0,1.1,0.01) pl ...