IntentService与Service的最大区别就是前者依次执行,执行完当前任务才执行下一个任务,后者并发执行

在IntentService里面不写onCreate方法

MainActivity:

 package com.zzw.test1;

 import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.SystemClock; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int value[] = new int[2];
for (int i = 1; i <= 20; i++) {
Intent intent = new Intent(this, TestAppIntentService.class);
value[0] = i;
value[1] = 20 - i;
intent.putExtra(Contants.KEY, value);
startService(intent);
}
} @Override
protected void onDestroy() {
super.onDestroy();
Intent intent = new Intent(this, TestAppIntentService.class);
stopService(intent);
} }

TestAppIntentService:

 package com.zzw.test1;

 import android.app.IntentService;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast; public class TestAppIntentService extends IntentService {
int count = 1; // 只能写空的构造方法
public TestAppIntentService() {
super("TestAppIntentService");
// TODO Auto-generated constructor stub
} // 相当于一个线程 不用在里面另外new一个线程
@Override
protected void onHandleIntent(Intent intent) {
Log.d("------", count + "-------开始");
int[] value = intent.getIntArrayExtra(Contants.KEY);
int sum = value[0] * value[1];
Log.d("-------------", value[0] + "*" + value[1] + "=" + sum);
Log.d("------", count + "-------结束");
count++;
} }

IntentService 串联 按顺序执行(此次任务执行完才执行下一个任务)的更多相关文章

  1. CountDownLatch用法---等待多个线程执行完才执行

    CountDownLatch用法---等待多个线程执行完才执行 CountDownLatch用法---等待多个线程执行完才执行 CountDownLatch用法---等待多个线程执行完才执行 Coun ...

  2. @DisallowConcurrentExecution 注解的作用 【定时器执行完当前任务才开启下一个线程的方式】

    转: @DisallowConcurrentExecution 注解的作用 2018年10月12日 16:42:40 fly_captain 阅读数:4317   Quartz定时任务默认都是并发执行 ...

  3. 分享一个shell脚本的坑:grep匹配+wc取值 在脚本执行后的结果与手动执行结果不一致

    打算在跳板机上写一个shell脚本,批量检查远程服务器上的main进程是否在健康运行中. 先找出其中一台远程机器,查看main进程运行情况 [root@two002 tmp]# ps -ef|grep ...

  4. update语句的执行步骤及commit语句的执行顺序

    update语句的执行步骤和其他DML语句的执行步骤是一样的包含insert .delete语句等,执行步骤如下: 一.如果数据和回滚数据不在数据库高速缓存区中,则oracle服务器进程将把他们从数据 ...

  5. js中页面加载完成后执行的几种方式及执行顺序

    1:使用jQuery的$(function){}; 2:使用jquery的$(document).ready(function(){});前两者本质上没有区别,第1种是第2种的简写方式.两个是docu ...

  6. ORACLE中查询语句的执行顺及where部分条件执行顺序测试

    Oracle中的一些查询语句及其执行顺序 原文地址:https://www.cnblogs.com/likeju/p/5039115.html 查询条件: 1)LIKE:模糊查询,需要借助两个通配符, ...

  7. js中页面加载完成后执行的几种方法及执行顺序

    在js和jquery使用中,经常使用到页面加载完成后执行某一方法.通过整理,大概是五种方式(其中有的只是书写方式不一样). 1:使用jQuery的$(function){}; 2:使用jquery的$ ...

  8. 使用定时器判断确保某个标签有值才执行方法, 控制js代码执行先后顺序

    使用定时器判断确保某个标签有值才执行方法: var wait = setInterval(function(){ var diqu = $("#diqu").val(); //确保 ...

  9. for里面是采用setInterval遍历二维数组,for循环到最后一个数的时候,才执行setInterval的问题解决

    点击播放看效果 <!doctype html> <html lang="en"> <head> <meta charset="U ...

随机推荐

  1. POJ 2157 Evacuation Plan [最小费用最大流][消圈算法]

    ---恢复内容开始--- 题意略. 这题在poj直接求最小费用会超时,但是题意也没说要求最优解. 根据线圈定理,如果一个跑完最费用流的残余网络中存在负权环,那么顺着这个负权环跑流量为1那么会得到更小的 ...

  2. gulp - connect

    Gulp plugin to run a webserver (with LiveReload) Install npm can help us to install the plugin. PS C ...

  3. 下载和编译 Open XML SDK

    我们需要一些工具来开始 Open XML 的开发. 开发工具 推荐的开发工具是 Visual Studio 社区版. 开发工具:Visual Studio Community 2013 下载地址:ht ...

  4. Python实现顺时钟回形矩阵

    无意间在网上看到了一个面试题是,写出一个回形矩阵.实现的效果大致如下: [ 1,   2,   3,   4, 5] [16, 17, 18, 19, 6] [15, 24, 25, 20, 7] [ ...

  5. Fiddler录制jmeter脚本,干货分享

    ​     我们知道以前jmeter的脚本来源有三个,手动书写.badboy录制.自带的录制功能(jmeter3.0该功能还比较好),目前我们又多了一个fiddler生成,自上次分享出来fiddler ...

  6. 基于jQuery的web在线流程图设计器GooFlow

    简易的流程图设计控件,效果图: JavaScript源文件在GooFlow.js中,样式文件是GooFlow2.css.可以自定义样式. GooFlow_item类是每个项的样式属性. 但估计实现任务 ...

  7. Android开发-API指南-<service>

    <service> 英文原文:http://developer.android.com/guide/topics/manifest/service-element.html 采集(更新)日 ...

  8. 阿里云搭建LAMP环境详细教程

    一.云服务器的选择 随着大数据时代的到来,为了满足更为复杂的运算和用户更为苛刻的需求,云计算应运而生,随之而来的就是云服务器.过去的服务器成本较高,运算模式较为单一,资源利用率不高,而云服务器的出现刚 ...

  9. The str method

    __str__ is a special method name, like __init__, that is supposed to return a string representation ...

  10. c语言描述简单的线性表,获取元素,删除元素,

    //定义线性表 #define MAXSIZE 20 typedef int ElemType; typedef struct { ElemType data[MAXSIZE]; //这是数组的长度, ...