本节学习IntentService, 可能就有人问了。 什么是IntentService, IntentService有什么作用? 不是已经有了Service,那为什么还要引入IntentService呢?

带着这两个问题,我们先来看一个样例:

我们新建一个MyIntentService样例:

public class MyIntentService extends IntentService {

	private int count = 5;
public MyIntentService() {
super("MyIntentService");
// TODO Auto-generated constructor stub
} @Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
Log.i("MyIntentService", "onBind------------");
return super.onBind(intent);
} @Override
public void onCreate() {
// TODO Auto-generated method stub
Log.i("MyIntentService", "onCreate------------");
super.onCreate();
} @Override
public void onDestroy() {
// TODO Auto-generated method stub
Log.i("MyIntentService", "onDestroy------------");
super.onDestroy();
} @Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
Log.i("MyIntentService", "onStart------------");
super.onStart(intent, startId);
} @Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
Log.i("MyIntentService", "onStartCommand------------");
return super.onStartCommand(intent, flags, startId);
} @Override
protected void onHandleIntent(Intent arg0) {
// TODO Auto-generated method stub while(count > 0)
{
//设置时间的输出方式
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time = format.format(new Date()); //显示时间
Log.i("MyService", time); try {
//延迟一秒
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} count--;
}
} }

当中IntentService是Service的子类。须要继承与Service。

在MyActivity中添加一个button。用于启动MyIntentService

		btn_intent.setOnClickListener(new OnClickListener() {

			@Override
public void onClick(View arg0) {
// 启动服务
Intent intent = new Intent(MyActivity.this, MyIntentService.class);
Log.i("MyActivity", "启动Intent服务button被按下!");
startService(intent);
}
});

执行效果例如以下:

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">

能够看到我们的界面没有出现卡住,(关于卡住不动。请看我Service二那节)。

同一时候打印5次后,自己主动destory。

既然现象已经看了,我总结一下:

Service与IntentService之间的差别

在这之前,须要知道Service的不足之处

a: Service不是专门的一条新的线程。因此不应该在Service中处理相当耗时的任务

b:service也不会专门的启动一个新的线程,Service与它所在的应用位于同一个进程中的。

所以,对于Service的2个缺陷,就引入了IntentService。

a:IntentService会使用一个队列来关联Intent的请求,每当Activity请求启动IntentService时,IntentService会将该请求增加一个队列。然后开启一个新的线程去处理请求。所以IntentService不会把主线程卡死

b:IntentService会创建单独的线程处理onHandleIntent()方法里的实现代码

c:同一时候IntentService不用重写onBind, OnStartCommand方法,仅仅需实现onHandleIntent()方法

d:当所以的请求处理完后,Intent后自己主动停止服务,无需手动停止服务

Android 四大组件学习之Service五的更多相关文章

  1. Android 四大组件学习之Service六

    上几节.我们学习怎样用StartServer启动一个服务,用bindServer去绑定一个服务.以及服务的生命周期,以及什么是IntentService. 也许有读者会发现,我们BindServer中 ...

  2. Android 四大组件学习之ContentProvider五

    上几节学习了ContentProvider的实际用途,读取短信.插入短信,读取联系人.插入联系人等. 本节课在学习ContentProvider的观察者. 在生活中有第三方的软件.比方什么短信软件.此 ...

  3. Android 四大组件学习之Server一

    上次学习了Android四大组件Activity之后,我们深刻理解了Activity.这次我们学习四大组件Service. Service与Activity的级别是一样的,都是Android系统不可缺 ...

  4. 入职小白随笔之Android四大组件——服务(Service)

    Service Android多线程编程 当我们在程序中执行一些耗时操作时,比如发起一条网络请求,考虑到网速等原因,服务器未必会立刻响应我们的请求,此时我们就需要将这些操作放在子线程中去运行,以防止主 ...

  5. android四大组件学习总结以及各个组件示例(1)

    android四大组件分别为activity.service.content provider.broadcast receiver. 一.android四大组件详解 1.activity (1)一个 ...

  6. Android四大组件初识之Service

    Service作为Android四大组件之一,可以与Activity建立双向连接(绑定模式),提供数据和功能.也能够接收Intent单方面请求(调用模式),进行数据处理和调度功能. Service与A ...

  7. [Android四大组件之二]——Service

    Service是Android中四大组件之一,在Android开发中起到非常重要的作用,它运行在后台,不与用户进行交互. 1.Service的继承关系: java.lang.Object → andr ...

  8. Android 四大组件学习之BroadcastReceiver一

    本节课学习四大组件最后一个, 广播接受者. 顾名思义广播接受者就是接受广播呗.比方在现实社会中,曾经每一个人家都有一台收音机,这可就能够去接受广播发出来的消息.大家都知道.程序世界也是參照的显示生活设 ...

  9. Android 四大组件之二(Service)

    service可以在和多场合的应用中使用,比如播放多媒体的时候用户启动了其他Activity这个时候程序要在后台继续播放,比如检测SD卡上文件的变化,再或者在后台记录你地理信息位置的改变等等,总之服务 ...

随机推荐

  1. MFC实现类似spy++dm取句柄功能

    处理WM_MOUSEMOVE消息 HANDLE_MSG( hwnd , WM_MOUSEMOVE, OnMouseMove ) 在OnMouseMove中, 设置SetCaputre() 移动鼠标到目 ...

  2. JSP页面通过c:forEach标签循环遍历List集合

    c:forEach>标签有如下属性: 属性 描述 是否必要 默认值items 要被循环的信息 否 无begin 开始的元素(0=第一个元素,1=第二个元素) 否 0end 最后一个元素(0=第一 ...

  3. python interview questions

    referce:python interview questions top 50 refercence:python interview questions top 15 summary Q: wh ...

  4. JavaScript递归简单实现个对象深拷贝

    JavaScript中对象的深拷贝来说一直都算比较恶心 毕竟没有什么api能直接全拷贝了 得自己便利写  最近在项目中需要深拷贝 自己简单封了个方法 话不多说 直接上码 <!DOCTYPE ht ...

  5. 深入Linux内核架构——进程管理和调度(上)

    如果系统只有一个处理器,那么给定时刻只有一个程序可以运行.在多处理器系统中,真正并行运行的进程数目取决于物理CPU的数目.内核和处理器建立了多任务的错觉,是通过以很短的间隔在系统运行的应用程序之间不停 ...

  6. Centos7 使用firewall管理防火墙

    一.Centos7使用firewall的管理防火墙 1.firewalld基本使用 启动:systemctl start firewalld 关闭:systemctl stop firewalld 状 ...

  7. Django之学员管理二

    Django之学员管理二 学生表的一对多的增删改查 views.py def students(request): #select students.sid,students.name,classes ...

  8. C语言学习11

    直接插入排序 //直接插入排序 #include <stdio.h> void main() { ], i; int insort(int a[], int n); printf(&quo ...

  9. Linux mint xfce 19 使用记录

    创建系统快照 创建系统快照是 Linux Mint 19 的重要建议,可以使用与更新管理器捆绑的 Timeshift 应用程序轻松完成创建与恢复. 这个阶段很重要,万一出现令人遗憾的事件,比如安装破坏 ...

  10. angularjs自己总结

    1.模块 自定的directive和controller需要在同一个model下,或者另外的model depModules他了. ng-app要等于model的名字,所有的directive要在下面 ...