Service 和 IntentService的区别;
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的区别;的更多相关文章
- Service和IntentService的区别
不知道大家有没有和我一样,以前做项目或者练习的时候一直都是用Service来处理后台耗时操作,却很少注意到还有个IntentService,前段时间准备面试的时候看到了一篇关于IntentServic ...
- Web Service和WCF的区别。其实二者不属于一个范畴!!!
Web Service和WCF的区别 [1]Web Service:严格来说是行业标准,也就是Web Service 规范. 它有一套完成的规范体系标准,而且在持续不断的更新完善中. 它使用XML扩展 ...
- Web Service和ISAPI的区别与联系 转
Web Service和ISAPI的区别与联系 1.Web Service 是一种新的web应用程序分支,他们是自包含.自描述.模块化的应用,可以发布.定位.通过web调用.Web Service ...
- Delphi Web Service和ISAPI的区别与联系 转
Web Service和ISAPI的区别与联系 1.Web Service 是一种新的web应用程序分支,他们是自包含.自描述.模块化的应用,可以发布.定位.通过web调用.Web Service ...
- 多线程、Service与IntentService的比较
资料摘自网络(侵删) Service Thread IntentService AsyncTask When to use ? Task with no UI, but shouldn't b ...
- Android之Service与IntentService的比较
Android之Service与IntentService的比较 不知道大家有没有和我一样,以前做项目或者练习的时候一直都是用Service来处理后台耗时操作,却很少注意到还有个IntentServ ...
- angularjs 中 Factory,Service,Provider 之间的区别
本片文章是使用了 angularjs 中使用 service 在controller 之间 share 对象和数据 的code(http://jsfiddle.net/kn46u0uj/1/) 来进行 ...
- [Android] Service和IntentService中显示Toast的区别
1. 表象 Service中可以正常显示Toast,IntentService中不能正常显示Toast,在2.3系统上,不显示toast,在4.3系统上,toast显示,但是不会消失. 2. ...
- Android Service、IntentService,Service和组件间通信
Service组件 Service 和Activity 一样同为Android 的四大组件之一,并且他们都有各自的生命周期,要想掌握Service 的用法,那就要了解Service 的生命周期有哪些方 ...
随机推荐
- css 新单位 fr
fr是css刚出的一个新的单位,目前经过测试在chrome和firefox是可以支持的 举个案列,拿一个网格布局来说吧 <!DOCTYPE html> <html lang=&quo ...
- nexus 私有 maven 仓库的搭建
下载地址 页面 : https://help.sonatype.com/repomanager3/download 首先需要安装jdk .安装棒法我 前面的文章有写.https://www.cnblo ...
- jmeter一些插件下载网址
JSONPathExtractor的插件下载 https://jmeter-plugins.org/wiki/JSONPathExtractor/
- Hive 特殊分隔符处理
HIVE特殊分隔符处理 Hive对文件中的分隔符默认情况下只支持单字节分隔符,,默认单字符是\001.当然你也可以在创建表格时指定数据的分割符号.但是如果数据文件中的分隔符是多字符的,如下图: 01| ...
- Linux shell脚本读取用户输入的参数
新建一个test.sh文件 #!/bin/sh echo "1 : For Test" echo "2 : For nohup &" whiletrue ...
- 网易云课堂Dubbo学习笔记
可以在这里下载dubbo的原码:https://github.com/alibaba/dubbo Dubbo架构原理:
- Mysql索引分类和索引优化
一. MySQL: 索引以B树格式保存 Memory存储引擎可以选择Hash或BTree索引,Hash索引只能用于=或<=>的等式比较. 1.普通索引:create index on Ta ...
- 用DLL实现插件的简单演示
这是DLL的代码 library MyDll; uses SysUtils, Dialogs, Classes; procedure ShowInfo(info:PChar);stdcall; beg ...
- linux 内存映射-ioremap和mmap函数
最近开始学习Linux驱动程序,将内存映射和ioremap,mmap函数相关资料进行了整理 一,内存映射 对于提供了MMU(存储管理器,辅助操作系统进行内存管理,提供虚实地址转换等硬件支持)的处理器 ...
- 测试教程网.unittest教程.8. 断言异常
From: http://www.testclass.net/pyunit/assert_raise/ 背景 我们有时候需要断言一些方法会抛出异常,这些异常需要符合我们的预期. 代码 新建test_e ...