在自定义的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. curl的概念及相关工具下载

    https://baike.so.com/doc/6746112-6960657.html https://curl.haxx.se/download.html https://curl.haxx.s ...

  2. Syntax error , insert “EnumBody” to complete EnumDeclaration

    当@Test写在方法前面的时候因为没有导入junit的jar包,如果已经导入架包依然是同样的错误,那就是方法没写对,或者还没有写方法

  3. Xshell 连接 CentOS 7 与 Ubuntu Server

    操作系统:windows 7 应用软件:Ware Workstation & Xshell 5 Linux:CentOS 7 Minimal & Ubuntu Server 16 == ...

  4. PID控制器(比例-积分-微分控制器)- V

    Linear Actuator - PID Control Introduction This application guide is designed to explain the basics ...

  5. (总结)Ubuntu apt-get apt-cache命令 使用

    http://rsljdkt.iteye.com/blog/1142463 apt-cache search wubipinyin apt-get命令本身并不具有管理软件包功能,只是提供了一个软件包管 ...

  6. apache 通过ajp访问tomcat多个站点

    copy mod_jk.so to modules下 httpd的配置项中添加如下内容 LoadModule proxy_module modules/mod_proxy.so LoadModule ...

  7. Go语言之高级篇beego框架之请求数据处理

    1.Controller中数据参数处理 获取参数:我们经常需要获取用户传递的数据,包括 Get.POST 等方式的请求,beego 里面会自动解析这些数据,你可以通过如下方式获取数据: GetStri ...

  8. mac下最简单的删除node方法是什么

    sudo rm -rf /usr/local/{bin/{node,npm},lib/node_modules/npm,lib/node,share/man/*/node.*}

  9. Nginx的安装和设置

    Nginx是一个高性能的HTTP服务器和反向代理服务器.当一个服务器访问量太大时(比如C10k问题,Concurrent 10,000 Connection),就可以安装设置一个Nginx服务器,将客 ...

  10. C#后台执行js

    StringBuilder sb = new StringBuilder(); sb.Append("<script type='text/javascript'>") ...