读书人

初学者初学Service

发布时间: 2012-09-11 10:49:03 作者: rapoo

菜鸟初学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。
[解决办法]
为了兼容

读书人网 >Android

热点推荐