Srevice不是在子线程,在Srevice中做耗时操作一样ANR,然后我们就会用到IntentService,IntentSrevice不但擅长做耗时操作,还有一个特点,用完即走;

在Srevice中做耗时轮询操作,使用Handler:


public class MyService extends Service { public MyService() {
} @Override
public IBinder onBind(Intent intent) {
throw new UnsupportedOperationException("Not yet implemented");
} @Override
public void onCreate() {
super.onCreate(); } private Handler mHandler = new Handler(){
@Override
public void dispatchMessage(Message msg) {
super.dispatchMessage(msg);
switch (msg.what){
case HANDLERSIGN:
Log.i(TAG, "dispatchMessage: "+args+(++num));
mHandler.sendEmptyMessageDelayed(HANDLERSIGN,HANDLERTIME);
if (num == 5){
AlertDialog.Builder builder = new AlertDialog.Builder(MyService.this);
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
MyService.this.stopSelf();
}
});
AlertDialog dialog = builder.create();
dialog.setMessage("我的计数"+num);
dialog.setTitle("提示");
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
dialog.show();
}
break;
}
}
}; private final String TAG = "ccb";
private String args;
private int num;
private final int HANDLERSIGN = 10;
private final int HANDLERTIME = 2010;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
args = intent.getStringExtra("args");
initData();
return super.onStartCommand(intent, flags, startId);
} private void initData() {
mHandler.sendEmptyMessageDelayed(HANDLERSIGN,HANDLERTIME);
} @Override
public void onDestroy() {
super.onDestroy();
Log.i(TAG, "onDestroy: 啊,ByKill");
mHandler.removeCallbacksAndMessages(null);
}
}

在IntentSrevice中做耗时轮询操作,可以任性到这种程度:


public class MyIntentService extends IntentService { public MyIntentService() {
super("MyIntentService");
} @Override
public void onCreate() {
super.onCreate();
} private final String TAG = "ccb";
private String args;
private int num;
@Override
protected void onHandleIntent(Intent intent) {
args = intent.getStringExtra("args");
initData();
} private void initData() {
for (int i = 0; i < 30; i++) {
try {
Thread.sleep(3000);
Log.i(TAG, "dispatchMessage: "+args+(++num));
if (num == 5) {
AlertDialog.Builder builder = new AlertDialog.Builder(MyIntentService.this);
AlertDialog dialog = builder.setPositiveButton("确定", null).create();
dialog.setMessage("我的计数" + num);
dialog.setTitle("我是服务");
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
dialog.show();
return;
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} @Override
public void onDestroy() {
super.onDestroy();
Log.i(TAG, "onDestroy: MyIntentService");
}
}

IntentSrevice中的onHandlerIntent()方法走完就会销毁掉自己,立马走onDestroy()方法;

Service 和 IntentService的区别;的更多相关文章

  1. Service和IntentService的区别

    不知道大家有没有和我一样,以前做项目或者练习的时候一直都是用Service来处理后台耗时操作,却很少注意到还有个IntentService,前段时间准备面试的时候看到了一篇关于IntentServic ...

  2. Web Service和WCF的区别。其实二者不属于一个范畴!!!

    Web Service和WCF的区别 [1]Web Service:严格来说是行业标准,也就是Web Service 规范. 它有一套完成的规范体系标准,而且在持续不断的更新完善中. 它使用XML扩展 ...

  3. Web Service和ISAPI的区别与联系 转

    Web Service和ISAPI的区别与联系   1.Web Service 是一种新的web应用程序分支,他们是自包含.自描述.模块化的应用,可以发布.定位.通过web调用.Web Service ...

  4. Delphi Web Service和ISAPI的区别与联系 转

    Web Service和ISAPI的区别与联系   1.Web Service 是一种新的web应用程序分支,他们是自包含.自描述.模块化的应用,可以发布.定位.通过web调用.Web Service ...

  5. 多线程、Service与IntentService的比较

    资料摘自网络(侵删)     Service Thread IntentService AsyncTask When to use ? Task with no UI, but shouldn't b ...

  6. Android之Service与IntentService的比较

    Android之Service与IntentService的比较  不知道大家有没有和我一样,以前做项目或者练习的时候一直都是用Service来处理后台耗时操作,却很少注意到还有个IntentServ ...

  7. angularjs 中 Factory,Service,Provider 之间的区别

    本片文章是使用了 angularjs 中使用 service 在controller 之间 share 对象和数据 的code(http://jsfiddle.net/kn46u0uj/1/) 来进行 ...

  8. [Android] Service和IntentService中显示Toast的区别

    1. 表象     Service中可以正常显示Toast,IntentService中不能正常显示Toast,在2.3系统上,不显示toast,在4.3系统上,toast显示,但是不会消失. 2. ...

  9. Android Service、IntentService,Service和组件间通信

    Service组件 Service 和Activity 一样同为Android 的四大组件之一,并且他们都有各自的生命周期,要想掌握Service 的用法,那就要了解Service 的生命周期有哪些方 ...

随机推荐

  1. 论 业务系统 架构 的 简化 (一) 不需要 MQ

    MQ , 就是 消息队列(Message Queue), 不知从什么时候起, MQ 被用来 搭建 分布式 业务系统 架构, 一个重要作用 就是用来  “削峰”   . 我们 这里 就来 讨论 如何 设 ...

  2. AES 加解密 Java实现

    package com.smt.cipher.symmetry; import java.nio.charset.Charset; import java.security.SecureRandom; ...

  3. Iris花逻辑回归与实现

    Iris花的分类是经典的逻辑回归的代表:但是其代码中包含了大量的python库的核心处理模式,这篇文章就是剖析python代码的文章. #取用下标为2,3的两个feture,分别是花的宽度和长度: # ...

  4. Spring 4 中重定向RedirectAttributes的使用

    RedirectAttributes 的使用 @RequestMapping(value = "/redirecttest", produces = "applicati ...

  5. mysql show master status为空值

    问题 执行show master status,输出结果为空: mysql> show master status; Empty set (0.00 sec) 原因 mysql没有开启日志. 查 ...

  6. 【转】类似py2exe软件真的能保护python源码吗

    类似py2exe软件真的能保护python源码吗 背景 最近写了个工具用于对项目中C/C++文件的字符串常量进行自动化加密处理,用python写的,工具效果不错,所以打算在公司内部推广.为了防止代码泄 ...

  7. CentOS7安装部署zabbix3.4操作记录

    CentOS7安装部署zabbix3.4操作记录 1.安装前准备 1.1 查看centos的系统版本 [root@zabbix ~]# cat /etc/redhat-release CentOS L ...

  8. CentOs6.7 python2.6升级到2.7.11

    1.查看当前python的版本 #python -V Python 2.6.6 2.下载Python-2.7.11 wget https://www.python.org/ftp/python/2.7 ...

  9. Windows Azure Virtual Machine (36) 扩展Azure ARM VM的磁盘大小

    <Windows Azure Platform 系列文章目录> 在默认情况下,Azure ARM VM的操作系统磁盘(OS Disk),容量为: (1)Windows VM OS Disk ...

  10. 黄聪:利用ImageMagicK给图片加水印

    1 图片水印处理 假设把名为logo.gif的水印图标添加在原始图片(src.jpg)右下角,且水印的下边缘距原始图片10像素.右边缘距原始图片5像素.使用如下命令即可: convert src.jp ...