当前位置: 代码迷 >> Android >> 初学者初学Service
  详细解决方案

初学者初学Service

热度:95   发布时间:2016-05-01 17:29:58.0
菜鸟初学Service
1. onStart() ,onStartCommand() instead.

As Follows :
-------------------------------------------------
Java code
onStart() public void onStart(Intent intent, int startId) {    }

--------------------------------------------------
onStartCommand:
Java code
    public int onStartCommand(Intent intent, int flags, int startId) {        onStart(intent, startId);         return mStartCompatibility ? START_STICKY_COMPATIBILITY : START_STICKY;    }

--------------------------------------------------
Google API
Java code
void     onStart(Intent intent, int startId)This method is deprecated. Implement onStartCommand(Intent, int, int) instead.onStart方式已经过时,被onStartCommand取代 int     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.

-------------------------------------------------
那么为何还会在onStartCommand中 调用onStart()方法?

---------------------------------------------------
他的启动顺序是 onCreate() ---> onStartCommand() ---> onStart() ,说明他却是调用了onStart()
注释掉 onStartCommand()后, 依然可以启动Service啊 !! onCreate() ---> onStart() 


观2.2源码中中 onStartCommand依然调用了onStart方法啊
既然都能正确的调用 那么onStart() 为何不推荐使用呢 ?

------解决方案--------------------
onStartCommand是新增加的方法。以前就是onStart。
------解决方案--------------------
为了兼容
  相关解决方案