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. java 中,for、for-each、iterator 区别

    java 中,for.for-each.iterator 区别: 无论是在数组中还是在集合中,for-Each加强型for循环都是它们各自的普通for循环的一种"简写方式",即两者 ...

  2. python 字符串格式化—format

    Python2.6 开始,新增了一种格式化字符串的函数 str.format().使用起来简单方便,不会遇到使用%时候格式的选择问题. 按照参数默认顺序 >>> "yesd ...

  3. springboot整合shiro-登录认证和权限管理

    https://blog.csdn.net/ityouknow/article/details/73836159

  4. 阿里云香港B区通过IPV6规避Google验证码

    最近买了阿里云香港B来FQ,然而被Google的验证码折磨的死去活来.四处查询,终于找到了一个合适的方案. 添加IPV6支持 阿里云香港是没有IPV6地址的,需要一个tunnel,这边使用HE.NET ...

  5. 解决web项目存在多个log4j.properties配置文件,导致日志级别配置不生效问题

    java开启log4j的debug模式 -Dlog4j.debug=true tomcat启动debug模式: linux打开catalina.sh导入: export JAVA_OPTS=" ...

  6. button高度改变

    代码:<input type="button" name="submit" value="submit" /> 利用css改变b ...

  7. paramiko 实现ssh登录和sftp登录

    简单ssh登录 import paramiko ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddP ...

  8. Thinkphp 关联模型

    1.定义关联模型 2.使用关联模型 D('BlogRelation')->relation(true)->add($data);

  9. <亲测>window+iis+letsencrypt+ssl 手动续期(通配符域名)

    之前提到过 window环境下 生成ssl通配符域名证方法为:https://www.cnblogs.com/duanweishi/p/9491209.html    下文为在window环境下手动续 ...

  10. C++11--右值引用(Perfect Forwarding)

    /* * 右值引用 2: Perfect Forwarding */ void foo( boVector arg ); // boVector既有移动构造又有拷贝构造 template< ty ...