Service 中的 onStart 和 onStartCommand
在自定义的service中,写了onStart和onStartCommand,
public class HttpWebService extends Service {
@Override
public void onCreate() {
super.onCreate();
.... @Override
public void onStart(Intent intent, int startId) {
// this will trigger AbstractBackgroundService.onStart()
super.onStart(intent, startId);
Log.d(LOG_TAG, "onStart" + intent);
} @Override
public int onStartCommand(Intent intent, int flags, int startId) {
int retVal = super.onStartCommand(intent, flags, startId);
Log.d(LOG_TAG, "onStartCommand" + intent); return retVal;
} }
如果用 bindService 这种方式调用,onStart和onStartCommand都不会被调用到
this.bindService(new Intent(this, HttpWebService.class),
new ServiceConnection() { @Override
public void onServiceConnected(ComponentName name,
IBinder service) {
HttpWebService webService = ((HttpWebService.LocalBinder) service)
.getService();
webService.SetServerListener(webListener);
} @Override
public void onServiceDisconnected(ComponentName name) { }
}, Context.BIND_AUTO_CREATE);
如果是用StartService调用,两个都被调用到
Intent intent = new Intent("com.example.sharefiles.services.ShareServices.Test");
intent.setClass(getApplicationContext(), HttpWebService.class);
intent.putExtra("enable", true);
this.startService(intent);
查看google 文档
http://developer.android.com/reference/android/app/Service.html
onStart(Intent intent, int startId)
onStartCommand(Intent, int, int) instead.onStartCommand(Intent intent, int flags, int startId)
startService(Intent), providing the arguments it supplied and a unique integer token representing the start request.鉴于目前的代码基本都是运行在API5 以上的,所以直接干掉onStart.
Service 中的 onStart 和 onStartCommand的更多相关文章
- android中activity向service中传值
和activity中互相传值类似 在activity中 Intent regIntent = new Intent(this, ChatService.class); regIntent.putEx ...
- 跟我学Windows Azure 四 Cloud Service中的WebRole与WorkRole,及他们之间的通信
Cloud Service 中WebRole就相当与我们的WebSite,而WorkRole相当与我们在服务器上写了个Windows Service,站在高可用的角度上来讲,Cloud Service ...
- Windows Service中使用Threading.Timer需注意回收
在Windows Service中使用Threading.Timer时需要注意回收池问题 Threading.Timer是基于线程池的,系统会对其进行垃圾回收. 当Threading.Timer定义在 ...
- 关于Service中bindService注意的几个问题
最近有用到Activity需要不断的从Service中获取数据,第一个想法肯定就是通过bind回调机制了,有几点概念模糊特此记录下: 单独使用bindService(),unbindService() ...
- Android悬浮框,在Service中打开悬浮窗;在Service中打开Dialog;
文章介绍了如何在Service中显示悬浮框,在Service中弹出Dialog,在Service中做耗时的轮询操作: 背景需求: 公司的项目现在的逻辑是这样的:发送一个指令,然后3秒一次轮询去查询这个 ...
- 宿主在Windows Service中的WCF(创建,安装,调用) (host到exe,非IIS)
1. 创建WCF服务 在vs2010中创建WCF服务应用程序,会自动生成一个接口和一个实现类:(IService1和Service1) IService1接口如下: using System.Ru ...
- Service stopSelf(int statId)和onStartcommand(Intent intent,int flags,int startId)
Stopping a service A started service must manage its own lifecycle. That is, the system does not sto ...
- Handler: Service中使用Toast
Handler 的使用在 android App 开发中用的颇多,它的作用也很大,使用 Handler 一般也会使用到多线程,相信大家对 Handler 不会陌生,在这里,重点说一下 android ...
- 如何托管ASP.NET Core应用到Windows Service中
(此文章同时发表在本人微信公众号"dotNET开发经验谈",欢迎右边二维码来关注.) 题记:正在构思一个中间件的设计,考虑是否既可以使用最新的技术,也可以兼顾传统的部署模式.所以有 ...
随机推荐
- 20172302 《Java软件结构与数据结构》第一周学习总结
2018下半年学习总结博客总目录:第一周 教材学习内容总结 第一章 概述 1.软件质量 软件工程(Software Engineering)是一门关于高质量软件开发的技术和理论的学科. 软件质量从以下 ...
- 当删除某一个jar包时tomcat中出现problem encountered while deleting resources问题
http://blog.csdn.net/u013226462/article/details/17715031
- window + php + memcached 安装
第一步:安装windows版memcached 下载地址:http://download.csdn.net/detail/zhaotengfei36520/8182503#comment 下载之后将文 ...
- DragonBones的下载和安装
DragonBones也称龙骨,是一款骨骼动画制作软件.DragonBones Pro是由Flash的龙骨骨骼动画插件进化而来的,与传统逐帧动画相比,骨骼动画资源较小且动画效果比较好. DragonB ...
- MySql md5加密 sqlserver md5加密 C# md5加密 java md5加密
便民md5加密: 百度md5加密: MySQL 加密语法: MD5(加密字符串) -- 中文加密 SELECT MD5('你好') -- 中文加密匹配查询 SELECT * FROM 表名 WHERE ...
- 19 个必须知道的 Visual Studio 快捷键
项目相关的快捷键 Ctrl + Shift + B = 生成项目 Ctrl + Alt + L = 显示Solution Explorer(解决方案资源管理器) Shift + Alt+ C = 添加 ...
- Java多线程:AQS
在Java多线程:线程间通信之Lock中我们提到了ReentrantLock是API级别的实现,但是没有说明其具体实现原理.实际上,ReentrantLock的底层实现使用了AQS(AbstractQ ...
- 设备树中ranges属性分析(1)
作者 彭东林 pengdonglin137@163.com 软件环境 Linux-4.10.17 Qemu+vexpress 概述 在设备树中有时会看到ranges属性,这个ranges属性可 ...
- springboot之启动原理解析及源码阅读
前言 SpringBoot为我们做的自动配置,确实方便快捷,但是对于新手来说,如果不大懂SpringBoot内部启动原理,以后难免会吃亏.所以这次博主就跟你们一起一步步揭开SpringBoot的神秘面 ...
- JavaScript 编码规范(中文/Airbnb公司版)
Airbnb 是一家位于美国旧金山的公司,本文是其内部的 JavaScript编码规范,写得比较全面,在 Github 上有 16,686 + Star,3,080 + fork,前端开发人员可参考. ...