在自定义的service中,写了onStart和onStartCommand,

public class HttpWebService extends Service {

@Override
public void onCreate() {
super.onCreate();

....@Overridepublic void onStart(Intent intent, int startId) {// this will trigger AbstractBackgroundService.onStart()super.onStart(intent, startId);Log.d(LOG_TAG, "onStart" + intent); }@Overridepublic 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() {@Overridepublic void onServiceConnected(ComponentName name,IBinder service) {HttpWebService webService = ((HttpWebService.LocalBinder) service).getService();webService.SetServerListener(webListener);}@Overridepublic 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. Service中的绑定服务总结

    绑定服务是客户端服务器接口中的服务器,绑定服务可以让组件绑定到服务.发送请求.接收响应,甚至执行进程间通信IPC,绑定服务通常只在为其他应用组件服务时处于活动状态,不会无限期在后台运行. 绑定服务是S ...

  2. 使用IntentService在Service中创建耗时任务

    IntentService是Service的子类,比普通的Service增加了额外的功能. Service存在两个问题: 1,Service不会专门启动一条单独的线程,Service与它所在应用访问者 ...

  3. 在后台Service中直接启动Activity

    如果你有Service在跑,但是没有处于resume的activity,这时候如果你想启动一个Activty,比如提醒用户升级应用,在后台Service中是可以直接启动Activity的 如果用的co ...

  4. Handler: Service中使用Toast

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

  5. 窗口设置Service中添加 浮动 View

    废话就不多说了,开始... 一般构建UI的时候都是通过Actvity.setContentView(R.layout.main) 来实现的,其等价于Activity.getWindow().setCo ...

  6. Android 在Service中使用bindService

    前面已经对Service的startServer方式启动一个服务了解过了,现在来看一下Service的另一种启动方式→bindServer bindServer使用场景 1.在同个app之间调用(即是 ...

  7. Service中bindService

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

  8. Android在service中实现随机数产生

    1.在service中实现随机数产生: 2.实现Service中的各个生命周期函数,并理解其功能: 2.在Activity界面实现随机数的显示,每2秒更新一次: 3.采用启动式完成service的启动 ...

  9. 在Service中通过WindowManger添加View的方式来把UI界面显示出来

    整体方案 在Service中通过WindowManger添加View的方式来把UI界面显示出来 业务场景 具体场景 IQOO手机,游戏辅助 这种场景能否使用Activity方式来做 使用activit ...

  10. 在IntentService中使用Toast与在Service中使用Toast的异同,intentservicetoast

    在IntentService中使用Toast与在Service中使用Toast的异同,intentservicetoast 1. 表象 Service中可以正常显示Toast,IntentServic ...

最新文章

  1. 033_jQuery Ajax的ajax方法
  2. selenium webdriver (12) -- 鼠标和键盘
  3. Android事件分发小结
  4. 第四章 前端开发——JQuery库
  5. Python从序列中选择k个不重复元素
  6. 条目十四《使用reserve来避免不必要的重新分配》
  7. 调查 10,500 名 Java 开发者发现,收费的 OracleJDK 仍是主流、IntelliJ IDEA 最受欢迎...
  8. [Java] 蓝桥杯ALGO-100 算法训练 整除问题
  9. Django根据现有数据库建立/更新model
  10. 新中大软件 java班不_新中大gsoft-12.0-软件安装说明.doc
  11. win7系统gpt分区激活工具哪个好?
  12. 借鸡生蛋术–砍价小程序的推广变现教学
  13. python爬虫设计实验
  14. PHP腾讯云短信接口
  15. Schematic export failed or was cancelled. Please consult the transcript in the source windo
  16. 学完了C++语法之后该学什么??(网络基础篇)
  17. 暑假多看看英文原版电影
  18. linux rpm -qip命令,linux rpm命令
  19. win10系统麦克风声音太小怎么设置
  20. objectArx ---基础操作

热门文章

  1. free 和 fclose
  2. unity player 显示播放错误时的解决办法
  3. HDU2837 Calculation(指数循环节)题解
  4. CodeForces-4C Registration system
  5. servlet中的字符编码过滤器的使用
  6. 深入Linux内核架构(中文版)pdf
  7. 《Cracking the Coding Interview》——第8章:面向对象设计——题目9
  8. Delphi Sql语句中值的引用
  9. 用R语言实现密度聚类dbscan
  10. 「专题训练」Collecting Bugs(POJ-2096)