在自定义的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)

This method was deprecated in API level 5. Implement onStartCommand(Intent, int, int) instead.

onStartCommand(Intent intent, int flags, int startId)

Called by the system every time a client explicitly starts the service by calling startService(Intent), providing the arguments it supplied and a unique integer token representing the start request.

鉴于目前的代码基本都是运行在API5 以上的,所以直接干掉onStart.

Service 中的 onStart 和 onStartCommand的更多相关文章

  1. android中activity向service中传值

    和activity中互相传值类似 在activity中 Intent regIntent = new Intent(this, ChatService.class);  regIntent.putEx ...

  2. 跟我学Windows Azure 四 Cloud Service中的WebRole与WorkRole,及他们之间的通信

    Cloud Service 中WebRole就相当与我们的WebSite,而WorkRole相当与我们在服务器上写了个Windows Service,站在高可用的角度上来讲,Cloud Service ...

  3. Windows Service中使用Threading.Timer需注意回收

    在Windows Service中使用Threading.Timer时需要注意回收池问题 Threading.Timer是基于线程池的,系统会对其进行垃圾回收. 当Threading.Timer定义在 ...

  4. 关于Service中bindService注意的几个问题

    最近有用到Activity需要不断的从Service中获取数据,第一个想法肯定就是通过bind回调机制了,有几点概念模糊特此记录下: 单独使用bindService(),unbindService() ...

  5. Android悬浮框,在Service中打开悬浮窗;在Service中打开Dialog;

    文章介绍了如何在Service中显示悬浮框,在Service中弹出Dialog,在Service中做耗时的轮询操作: 背景需求: 公司的项目现在的逻辑是这样的:发送一个指令,然后3秒一次轮询去查询这个 ...

  6. 宿主在Windows Service中的WCF(创建,安装,调用) (host到exe,非IIS)

    1. 创建WCF服务 在vs2010中创建WCF服务应用程序,会自动生成一个接口和一个实现类:(IService1和Service1) IService1接口如下:   using System.Ru ...

  7. 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 ...

  8. Handler: Service中使用Toast

    Handler 的使用在 android App 开发中用的颇多,它的作用也很大,使用 Handler 一般也会使用到多线程,相信大家对 Handler 不会陌生,在这里,重点说一下 android ...

  9. 如何托管ASP.NET Core应用到Windows Service中

    (此文章同时发表在本人微信公众号"dotNET开发经验谈",欢迎右边二维码来关注.) 题记:正在构思一个中间件的设计,考虑是否既可以使用最新的技术,也可以兼顾传统的部署模式.所以有 ...

随机推荐

  1. bootstrap中的对话框-dialog

    <!DOCTYPE html><html> <head> <meta charset="UTF-8"> <meta name= ...

  2. php中静态方法的使用

    静态方法 (1)静态方法不能访问这个类中的普通属性,因为那些属性属于一个对象,但可以访问静态属性: (2)从当前类(不是子类)中访问静态方法或属性,可以使用 self 关键字,self 指向当前类,就 ...

  3. Qt 4.6.2静态编译

    一.下载mingw 4.4.0:ftp://ftp.trolltech.com/misc/MinGW-gcc440_1.zip 二.解压到C:\mingw目录下,设置环境变量path将C:\mingw ...

  4. ES-常见搜索方式

    1.query string search 2.query DSL 3.query filter 4.full-text search 5.phrase search 6.highlight sear ...

  5. Cocos Creator采坑:原来使用Cocos Creator做游戏,是有服务器!!!

    我傻傻的以为,我们没有服务器. 今天上传测试代码,测试才发现! 原来我们真的是有服务器的!只不过是一个本地的服务器~!需要服务器打开,然后,扫码才能访问!! 为了证明我们是有服务器的,我做了一下测试 ...

  6. 使用 IntraWeb (33) - Cookie

    在 IW.HTTP.Cookie 单元提供有两个相关类: THTTPCookie.TCookieList; 另外 IWServerController 还有一个 CookieOptions 选项. 但 ...

  7. 使用AngularJS中的filterFilter函数进行过滤

    AngularJS中有一个filterFilter函数用来对集合过滤,非常方便. 源代码大致如下: function filterFilter(){ return function(aray, exp ...

  8. 【linux】linux下对java程序生成dump文件,并使用IBM Heap Analyzer进行分析,查找定位内存泄漏的问题代码

    1.首先,java程序启动在linux,怎么生成dump文件? 1>第一步,首先你需要得到java程序的PID,最简单的方法使用如下命令 ps -ef|grep java 或者如果是docker ...

  9. CentOS 7 NAT软路由

    ☼ NAT 转发软路由 开启 NAT 转发之后,只要本机可以上网,不论是单网卡还是多网卡,局域网内的其他机器可以将默认网关设置为已开启 NAT 转发的服务器 IP ,即可实现上网. 信任所有连接,并且 ...

  10. centos 7下独立的python 2.7环境安装

    之前写过python 3.x的独立安装,相对来说比较简单直接安装即可,因为对于系统默认的python版本是2.x,那么安装3.x的时候只需要编译到新的目录即可,所有的模块都不会和之前的2有联系,但是如 ...