在自定义的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. 本地Sql Server数据库传到服务器数据库

    将网站项目上传到服务器时,会遇到本地数据库该如何上传的问题.下面在西部数码购买的虚拟主机的基础上,解决数据库上传问题.   1.在西部数码购买虚拟主机后,会赠送了一个数据库,该数据库就可以作为网站项目 ...

  2. C#高级编程9-第12章 动态语言扩展

    C#高级编程9-第12章 动态语言扩展 dynamic t = new ExpandoObject(); t.Abc = "abc"; t.Value = ; Console.Wr ...

  3. 更改mac系统语言及其软件

    如图,把所用语音置顶 https://support.apple.com/zh-cn/HT202036 1.firefox需要下载英文版,同时可以保留中文版,两个不能同时启动.如果你只是为了切换浏览器 ...

  4. 微信小程序wx.switchTab传参问题

    业务背景:从提问跳到列表需要刷新,以显示刚提交的数据. 但是官方文档 wx.switchTab 明确指明路径后是不能带参数的,怎么办? 网上有很多解决方案是:switchTab成功跳转后调用succe ...

  5. 你应该更新的 Java 知识之常用程序库【转载】

    在很多人眼中,Java 已经是一门垂垂老矣的语言,但并不妨碍 Java 世界依然在前进.如果你曾离开 Java,云游于其它世界,或是每日只在遗留代码中挣扎,或许是时候抬起头,看看老 Java 中的新东 ...

  6. vmware虚拟机环境下配置centos为静态IP的步骤

    一.修改centos配置文件 配置文件位于/etc/sysconfig/network-scripts/ifcfg-eth0 修改的内容为: ONBOOT=yes BOOTPROTO=static I ...

  7. password、文件MD5加密,passwordsha256、sha384、sha512Hex等加密

    package encryption; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io. ...

  8. [转]POJ3624 Charm Bracelet(典型01背包问题)

    来源:https://www.cnblogs.com/jinglecjy/p/5674796.html 题目链接:http://bailian.openjudge.cn/practice/4131/ ...

  9. curl重写php file_get_contents

    file_get_contents在连接不上的时候会提示Connection refused,有时候会带来不便:另外,curl的性能比file_get_contents高,所以用curl重写file_ ...

  10. MAC EI Capitan上更新系统自带SVN版本号(关闭SIP方能sudo rm)

    继昨晚之后.决定更新系统自带的svn.自带的svn版本号是1.7.看官网svn:http://www.wandisco.com/subversion/download#osx 最新版本号是1.9.13 ...